解决Dom4j中"The Node already has an existing parent"问题

在xml开发中经常会需要将一个Document的部分元素复制到另一个Document中,但dom4j中直接add会产生“The Node already has an existing parent”异常。
下面是我的示例,要将image节点合成如下文档形式。
<?xml version="1.0" encoding="UTF-8"?>
<data>
	<deviceId>device006</deviceId>
	<deviceType>QDZYDP</deviceType>
	<images> 
		<image id="0" name="device006_1.png" width="240" height="144"> 
			<lines>		
				<line id="1" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="27" y="36.5"/>
							<endPoint x="205" y="36.5"/>
						</subLine>		
					</subLines>	
				</line>	
				<line id="2" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="1" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="27" y="56.5"/>
							<endPoint x="84" y="56.5"/>
						</subLine>		
					</subLines>	
				</line>		
				<line id="3" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="1" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="83" y="56"/>
							<endPoint x="111" y="43"/>
						</subLine>		
					</subLines>	
				</line>	
				<line id="4" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="27" y="109.5"/>
							<endPoint x="205" y="109.5"/>
						</subLine>		
					</subLines>	
				</line>	
				<line id="5" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="34.5" y="29"/>
							<endPoint x="34.5" y="117"/>
						</subLine>		
					</subLines>	
				</line>	
				<line id="6" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="3" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="112.5" y="29"/>
							<endPoint x="112.5" y="117"/>
						</subLine>		
					</subLines>	
				</line>	
				<line id="7" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="197.5" y="29"/>
							<endPoint x="197.5" y="117"/>
						</subLine>		
					</subLines>	
				</line>																			
			</lines>
		</image> 
		<image id="3" name="device006_2.png" width="240" height="144"> 
			<lines>		
					
				<line id="1" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="127" y="79"/>
							<endPoint x="162" y="115"/>
						</subLine>		
					</subLines>	
				</line>															
				<line id="2" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="1" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="11" y="50.5"/>
							<endPoint x="98" y="50.5"/>
						</subLine>		
					</subLines>	
				</line>	
				<line id="3" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="3" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="122" y="34.5"/>
							<endPoint x="172" y="34.5"/>
						</subLine>		
					</subLines>	
				</line>	
				<line id="4" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="11" y="78.5"/>
							<endPoint x="172" y="78.5"/>
						</subLine>		
					</subLines>	
				</line>	
				<line id="5" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="1" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="11" y="113.5"/>
							<endPoint x="172" y="113.5"/>
						</subLine>		
					</subLines>	
				</line>	
				<line id="6" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="3" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="18.5" y="43"/>
							<endPoint x="18.5" y="125"/>
						</subLine>		
					</subLines>	
				</line>	
				<line id="7" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="3" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="125.5" y="27"/>
							<endPoint x="125.5" y="78"/>
						</subLine>		
					</subLines>	
				</line>	
				<line id="8" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="3" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="164.5" y="27"/>
							<endPoint x="164.5" y="121"/>
						</subLine>		
					</subLines>	
				</line>																								<line id="9" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
					<subLines>
						<subLine lineType="1"> 
							<beginPoint x="96" y="50"/>
							<endPoint x="125" y="33"/>
						</subLine>		
					</subLines>	
				</line>											
			</lines>
		</image> 													
	</images>
</data>		

public static String addImageNode(String xml1, String xml2, int id) {
		if (xml1 != null && !xml1.equals("")) {
			Document document1 = getUTFDocument(xml1);
			Document document2 = getUTFDocument(xml2);
			List dataNode = document1.selectNodes("/data/images");
			Element e = (Element) dataNode.get(0);
			List imagelist = document2.selectNodes("/data/images/image");
			Element e2 = (Element) imagelist.get(0);
			Attribute ima = e2.attribute("id");
			ima.setValue(String.valueOf(id));
//			dataNode.add(e2);
		
			e.add((Element)e2.clone());
[color=red]//			e.addElement(e2.getName()).appendContent(e2); [/color]
			return outPutXML(document1);
		} else {
			Document document2 = getUTFDocument(xml2);
			List dataNode = document2.selectNodes("/data/images/image");
			Element e = (Element) dataNode.get(0);
			Attribute ima = e.attribute("id");
			ima.setValue(String.valueOf(id));
			return outPutXML(document2);
		}

	}


红色部分也可以添加,但是会丢失属性。

你可能感兴趣的:(xml)