我有一个输入xml文件,我需要复制元素和标签中的元素,但是问题是<Name>
节点中还有另一个名为<Name>
的标签。在这种情况下我该如何复制?
Input.xml
<?xml version="1.0" encoding="utf-8" ?>
<data>
<Name>
<College name="test1">
<Name>
<dummyData1>dummy1</dummyData1>
<dummyData2>dummy2</dummyData2>
</Name>
</College>
<SomeData>SomeValue</SomeData>
</Name>
<SomeOtherTags>Not needed</SomeOtherTags>
<Address>
<Country name="test2">
<City name = "city1">
<zip>
<dummy3>dummy3</dummy3>
</zip>
</City>
</Country>
</Address>
</data>
Base.xml
<?xml version="1.0" encoding="utf-8" ?>
<data>
<Name>
</Name>
<Address>
</Address>
</data>
这是我的期望
Output.xml
<?xml version="1.0" encoding="utf-8" ?>
<data>
<Name>
<College name="test1">
<Name>
<dummyData1>dummy1</dummyData1>
<dummyData2>dummy2</dummyData2>
</Name>
</College>
<SomeData>SomeValue</SomeData>
</Name>
<Address>
<Country name="test2">
<City name = "city1">
<zip>
<dummy3>dummy3</dummy3>
</zip>
</City>
</Country>
</Address>
</data>
添加缺少的代码,谢谢Andreas
Document outputDoc = db.parse(baseXml);
Document inputDoc = db.parse(inputXml);
Node outputNameNode = outputDoc.getElementsByTagName("Name").item(0);
Node outputAddressNode = outputDoc.getElementsByTagName("Address").item(0);
NodeList nameNodeList = inputDoc.getElementsByTagName("Name");
System.out.println(nameNodeList.getLength());
for (int i = 0; i < nameNodeList.getLength(); i++) {
System.out.println(nameNodeList.item(i));
Node outputNode = outputDoc.importNode(nameNodeList.item(i), true);
outputNameNode.appendChild(outputNode);
}