Java从XML文件中删除子项

时间:2014-03-25 19:13:27

标签: java xml xmldom

我想从XML文件中删除一个子项,当然在修改后保存该文件。 这是我的XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><Customers>
    <Marker>
        <title>ABB EMEA</title>
    </Marker>
    <Marker>
        <title>AllScripts</title>
    </Marker>
    <Marker>
        <title>ARRIS (Motorola)</title>
    </Marker>
    <Marker>
        <title>ARRIS (RWC)</title>
    </Marker>
    <Marker>
        <title>BHS</title>
        <site_location>Weinhammer, Hofhalde, Konstanz, Germany</site_location>
    </Marker>
    <Marker>
        <title>Durst</title>
        <site_location>Brixen, Italy</site_location>
    </Marker>
    <Marker>
        <title>EMEA DEMO</title>
        <site_location>AWS could</site_location>
    </Marker>
    <Marker>
        <title>Harris</title>
    </Marker>

</Customers>

我想完成删除拥有&#39;标题&#39; - &#39; Durst&#39;。

这是我的代码:

public static void Rebuild_Cords_XML (File ff)
{
    try
    {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(ff);

        NodeList nodes = doc.getElementsByTagName("Marker");
        int x=0;
        for(int i=0;i<nodes.getLength();i++)
        {
            Node nNode = nodes.item(i);

            if (nNode.getNodeType() == Node.ELEMENT_NODE)
            {
                Element eElement = (Element) nNode;
                if(eElement.getElementsByTagName("title").item(0).getTextContent().equals(("Durst")));
                {

                }
            }
        }


     // write the DOM object to the file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
                    Transformer transformer=null;
                    try {
                        transformer = transformerFactory.newTransformer();
                    } catch (TransformerConfigurationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    DOMSource domSource = new DOMSource(doc);
                    StreamResult streamResult = new StreamResult(ff);
                    try {
                        transformer.transform(domSource, streamResult);
                    } catch (TransformerException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


    }
    catch (ParserConfigurationException | IOException | SAXException e) {
        ;}
}

正如您所看到的,我能够手动检测到它,但我不知道要将其删除,尝试访问父节点会删除其他标记&#39;孩子但不是必需的。

请协助。

1 个答案:

答案 0 :(得分:1)

我不完全确定你要删除的是什么,所以这里有两种方式。

这将删除包含标题durst的整个标记:

        for (int i = 0; i < nodes.getLength(); i++) {
            Node nNode = nodes.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                if (eElement.getElementsByTagName("title").item(0).getTextContent().equals(("Durst"))) {
                    nNode.getParentNode().removeChild(nNode);
                }
            }
        }

这将仅删除标记中的标题标记:

        for (int i = 0; i < nodes.getLength(); i++) {
            Node nNode = nodes.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                if (eElement.getElementsByTagName("title").item(0).getTextContent().equals(("Durst"))) {
                    nNode.removeChild(eElement.getElementsByTagName("title").item(0));
                }
            }