代码之家  ›  专栏  ›  技术社区  ›  dan2k3k4

在Java中将元素从一个XML文件复制并重命名为另一个XML文件

  •  2
  • dan2k3k4  · 技术社区  · 14 年前

    我可以使用 “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  
    

    如果我稍微移动一下代码,就会出现其他错误,比如: 等。


    干杯!

    1 回复  |  直到 8 年前
        1
  •  1
  •   dogbane    14 年前

    为什么要使用Oracle XML解析器?

    如果使用 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>