使用Java更新xml文件

时间:2016-08-08 07:05:11

标签: java xml dom xml-parsing

我有一个初始文件: file1.xml

<products>
    <product>
        <code>100A</code>
    </product>
    <product>
        <code>100B</code>
    </product>
    <product>
        <code>101B</code>
    </product>
    <product>
        <code>101D</code>
    </product>
    <product>
        <code>102E</code>
    </product>
    </products>

file2.xml

<amounts>
<amount>
<sku>100A</sku>
<price>15.29</price>
</amount>
<amount>
<sku>100B</sku>
<price>16.29</price>
</amount>
<amount>
<sku>101B</sku>
<price>25.29</price>
</amount>
<amount>
<sku>101D</sku>
<price>14.29</price>
</amount>
<amount>
<sku>102E</sku>
<price>12.29</price>
</amount>
</amounts>

我想删除SKU上的file1.xml字母,如果2个产品有相同的sku,则删除最后一个,更新也应该反映在文件2中。 最终输出将如下:

file1.xml

<products>
    <product>
        <code>100</code>
    </product>
    <product>
        <code>101</code>
    </product>
    <product>
        <code>102</code>
    </product>
    </products>

file2.xml

<amounts>
<amount>
<sku>100</sku>
<price>15.29</price>
</amount>
<amount>
<sku>101</sku>
<price>25.29</price>
</amount>
<amount>
<sku>102</sku>
<price>12.29</price>
</amount>
</amounts>

我尝试使用以下代码但我无法从file1.xml中删除产品节点

try {
    String filepath = "file1.xml";
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(filepath);

    Node products = (Node) doc.getFirstChild();
    NodeList nl = products.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Element el1 = (Element) nl.item(i);
            if (el1.getNodeName().contains("product")) {
                String codeValue = el1.getElementsByTagName("code").item(0).getTextContent();

                for (int j = i + 2; j < nl.getLength(); j = j + 2) {
                    Element el2 = (Element) nl.item(j);
                    if (el2.getNodeName().contains("product")) {

                        String sibling = el2.getElementsByTagName("code").item(0).getTextContent();
                        if (codeValue.equals(sibling)) {

                            products.removeChild(nl.item(j));

                        }

                    }
                }



            }
        }


    }
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(filepath));
    transformer.transform(source, result);

} catch (ParserConfigurationException pce) {
    pce.printStackTrace();
} catch (TransformerException tfe) {
    tfe.printStackTrace();
} catch (IOException ioe) {
    ioe.printStackTrace();
} catch (SAXException sae) {
    sae.printStackTrace();
}
}

}

我得到的例外如下:

线程中的异常&#34; main&#34; java.lang.ClassCastException:com.sun.org.apache.xerces.internal.dom.DeferredTextImpl无法强制转换为org.w3c.dom.Element     在org.sam.parser.test.sampleDups.main(sampleDups.java:43)

0 个答案:

没有答案