我可以使用“org.w3c.dom.Document将一个节点从一个XML文件复制到另一个XML文件。 importNode(Node importedNode,boolean deep)”
但是,我似乎无法重命名我正在复制的元素。
我有类似的东西:
File1.xml
<SomeCustomNode randomAttribute="aValue" another="10/10/2010">
<Information>
<Yellow name="banana"/>
<Orange name="orange"/>
<Red name="strawberry"/>
</Information>
<Some>
<IgnoredNode/>
</Some>
</SomeCustomNode>
等等:
FileList.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
并向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
如果我将代码移动一点,我会得到其他错误,例如: 无法删除或替换节点,它不是当前节点的子节点等。
通过XSLT会更好吗?我正在做的就是获取一个特定节点(以及它的子节点)并将其放入另一个XML文件中,但替换Element名称并添加2个属性(带有值)。它将是每个文件(File1 ... File ###)的相同节点,并且将以相同的方式重命名,属性值取自源文件(例如,我的示例中为File1.xml)和子文件节点不会更改(在我的示例中为黄色,橙色,红色) 干杯!
答案 0 :(得分:1)
为什么使用Oracle XML Parser?
如果您使用javax.xml
提供的默认值,则不会出现此错误:
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.sun.org.apache.xpath.internal.XPathAPI;
public static void main(String[] args) throws Exception {
Document docFile1 = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("File1.xml"));
Document docFileList = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("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");
print(docFileList);
}
打印出来:
<ListOfNodes date="12/10/2010">
<Information>
<Yellow name="banana"/>
<Orange name="orange"/>
<Red name="strawberry"/>
</Information>
<aCopy name="vegetables" version="3">
<Yellow name="sweetcorn"/>
<Orange name="carrot"/>
<Red name="tomato"/>
</aCopy>
</ListOfNodes>