有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我可以使用org.w3c.dom.Document将一个节点从一个XML文件复制到另一个XML文件。importNode(节点importedNode,布尔深度)
但是,我似乎无法重命名正在复制的元素

我有这样的想法:
文件1。xml

<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表达式获取信息节点作为节点列表(仅1个结果),然后将其导入到文件2中,如下所示:

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文件中,但替换元素名并添加两个属性(使用值)。对于每个文件(File1…file######),它都是相同的节点,并且将以相同的方式重命名,属性值取自源文件(例如,对于我的示例,是File1.xml),子节点不会更改(在我的示例中是黄色、橙色、红色)
干杯


共 (1) 个答案

  1. # 1 楼答案

    为什么要使用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>