我可以使用
“org.w3c.dom.Document.文档。
importNode(节点importedNode,布尔深度)
"
我有点像:
<SomeCustomNode randomAttribute="aValue" another="10/10/2010">
<Information>
<Yellow name="banana"/>
<Orange name="orange"/>
<Red name="strawberry"/>
</Information>
<Some>
<IgnoredNode/>
</Some>
</SomeCustomNode>
像这样的:
文件列表.xml
<ListOfNodes date="12/10/2010">
<aCopy name="fruit" version="10">
<Yellow name="banana"/>
<Orange name="orange"/>
<Red name="strawberry"/>
</aCopy>
<aCopy name="vegetables" version="3">
<Yellow name="sweetcorn"/>
<Orange name="carrot"/>
<Red name="tomato"/>
</aCopy>
</ListOfNodes>
所以,我要做的是从
File1.xml
把它插入
FileList.xml
Element
并向元素添加一些属性。
问询处
aCopy name="fruit" version="10"
我目前正在使用一个XPath表达式来获取
问询处
节点作为NodeList(仅1个结果),然后将其导入到File2中,如下所示:
Document docFile1 = XMLDocumentStore.getDoc("/path/to/File1.xml");
Document docFileList = XMLDocumentStore.getDoc("/path/to/FileList.xml");
NodeList result = XPathAPI.selectNodeList(docFile1.getFirstChild(), ".//Information");
Node importNode = docFileList.importNode(result.item(0), true);
// We want to replace aCopy fruit with the updated version found in File1
NodeList fruitNode = XPathAPI.selectNodeList(docFileList.getFirstChild(), ".//aCopy[@name=\"fruit\"]");
Node replaceNode = fruitNode.item(0).getParentNode().replaceChild(importNode, fruitNode.item(0)); // probably a better way to do this
// Now we want to replace the Element name as it is still set to Information
docFileList.renameNode(replaceNode, null, "aCopy"); // Error: oracle.xml.parser.v2.XMLDOMException: cannot add attribute belonging to another element
如果我稍微移动一下代码,就会出现其他错误,比如:
等。
干杯!