`

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

XML 
阅读更多
在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);
		}

	}


红色部分也可以添加,但是会丢失属性。
分享到:
评论

相关推荐

    dom4jdemo例子

    DOM4J是一个非常灵活的Java XML API,它提供了全面的XML解决方案,包括XML解析、文档创建、修改以及XPath查询等功能。它不仅支持SAX和DOM这两种主流的XML处理模型,还引入了更为简洁的事件驱动模型。DOM4J的设计目标...

    510(k)-for-a-Change-to-an-Existing-Device.pdf

    a Change to an Existing Device Guidance for Industry and Food and Drug Administration Staff Document issued on October 25, 2017. The draft of this document was issued on August 8, 2016. This ...

    Deciding-When-to-Submit-a-510(k)-for-a-Software-Change-to-an-Existing-Device.pdf

    510(k) for a Software Change to an Existing Device ______________________________________________________________________________ Guidance for Industry and Food and Drug Administration Staff ...

    Refactoring: Improving the Design of Existing Code 第二版

    This eagerly awaited new edition has been fully updated to reflect crucial changes in the programming landscape.Refactoring, Second Edition,features an updated catalog of refactorings and includes ...

    expect4j.jar 1.0

    Expect4j is an attempt to rewrite Expect in Java and provide bindings to the TclJava interpreter. The goal is to be able to port existing Tcl code bases that use Expect directly to Java without ...

    ORA-65122_ Pluggable database GUID conflicts with the GUID

    ORA-65122_ Pluggable database GUID conflicts with the GUID of an existing_ITPUB博客.mhtml

    Refactoring Improving The Design Of Existing Code

    Refactoring Improving The Design Of Existing Code

    Refactoring Improving the Designof Existing Code(中英文版)

    《Refactoring: Improving the Design of Existing Code》是软件开发领域的一本经典著作,由Martin Fowler著述。这本书深入探讨了重构这一关键的软件工程实践,对于Java开发者来说尤其具有指导意义。重构是指在不...

    Node.js Design Patterns Second Edition[July 2016]

    You will gain an understanding of the basic building blocks and principles of writing large and well-organized Node.js applications and you will be able to apply these principles to novel problems ...

    Learning.Node.js.for..NET.Developers.epub

    This short guide will help you develop applications using JavaScript and Node.js, leverage your existing programming skills from .NET or Java, and make the most of these other platforms through ...

    Refactoring: Improving the Design of Existing Code

    通过实例和经验分享,作者们展示了如何在代码库中发现和解决设计问题,提升软件的长期质量和可持续性。同时,他们强调重构不仅仅是技术操作,更是一种思维方式,需要开发者具备对代码结构和设计模式的深入理解。 ...

    Rename an Existing Visual Studio project

    标题提到的"Rename an Existing Visual Studio project"是一个关于如何更改已创建的VS项目名称的主题。这个过程涉及到对项目文件、解决方案文件以及引用等多方面的修改,以确保重命名后的项目能够正常运行。 首先,...

    refactoring-improving the design of existing code.pdf

    1. **识别代码坏味道**:书中列举了多种常见的代码坏味道,如“长方法”、“重复代码”、“开关病例”等,并提供了识别这些问题的线索。 2. **重构模式**:Martin Fowler 提供了一系列的重构模式,每个模式都是一个...

    Refactoring(Improving the Design of Existing Code)

    识别和解决代码异味是重构过程中的重要环节。作者通过大量实际案例,指导读者如何识别不良代码的模式,并通过一系列的重构手法进行改善。 重构的目录中包含了大量的重构手法,每一种手法都用“之前”和“之后”的...

    Refactoring Improving The Design of Existing Code

    Refactoring Improving The Design of Existing Code 重构已有代码

    下载覆盖已经存在的文件。「Downloads Overwrite Already Existing Files」-crx插件

    请参阅此处的源代码:https://github.com/zach-adams/downloads-overwrite-already-existing-files我不对丢失的数据负责。这不会在覆盖之前比较文件的内容,而只是比较文件名。 支持语言:English (United States)

    docx4j-3.3.6

    docx4j is an open source (Apache v2) library for creating, editing, and saving OpenXML "packages", including docx, pptx, and xslx. It uses JAXB to create the Java representation. Open existing docx/...

    Learning Node.js for .NET Developers(PACKT,2016)

    This short guide will help you develop applications using JavaScript and Node.js, leverage your existing programming skills from .NET or Java, and make the most of these other platforms through ...

Global site tag (gtag.js) - Google Analytics