无法删除XML中的特定节点

时间:2016-05-18 18:59:28

标签: java xml

我有一个XML文件,我需要删除一个特定的节点。将基于逻辑动态地定义要删除的节点。我一直在互联网上寻找解决方案,但仍无法删除我的节点。我收到错误 - NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist

以下是XML文件示例。我需要删除<NameValuePairs>的节点<name>Local Variables</name>。下面是我的XML文件Java代码示例

示例XML文件

<?xml version="1.0" encoding="UTF-8"?>
<DeploymentDescriptors xmlns="http://www.tibco.com/xmlns/dd">
<name>Test</name>
<version>1</version>
<DeploymentDescriptorFactory>
    <name>RepoInstance</name>
</DeploymentDescriptorFactory>
<DeploymentDescriptorFactory>
    <name>NameValuePairs</name>
</DeploymentDescriptorFactory>
<NameValuePairs>
    <name>Global Variables</name>
    <NameValuePair>
        <name>Connections1</name>
        <value>7222</value>
        <requiresConfiguration>true</requiresConfiguration>
    </NameValuePair>
    <NameValuePair>
        <name>Connections2</name>
        <value>7222</value>
        <requiresConfiguration>true</requiresConfiguration>
    </NameValuePair>
</NameValuePairs>
<NameValuePairs>
    <name>Local Variables</name>
    <NameValuePair>
        <name>Connections3</name>
        <value>8222</value>
        <requiresConfiguration>true</requiresConfiguration>
    </NameValuePair>
    <NameValuePair>
        <name>Connections3</name>
        <value>8222</value>
        <requiresConfiguration>true</requiresConfiguration>
    </NameValuePair>
</NameValuePairs>
</DeploymentDescriptors>

Java代码

File fDestFile = new File("myfile.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document oDoc3 = dBuilder.parse(fDestFile);
NodeList oDestFlowList = oDoc3.getElementsByTagName("NameValuePairs");
for (int m = 0; m < oDestFlowList.getLength(); m++) {
     NodeList oDestchildList = oDestFlowList.item(m).getChildNodes();
     for (int n = 0; n < oDestchildList.getLength(); n++) {
          Node oDestchildNode = oDestchildList.item(n);
          if ("name".equals(oDestchildNode.getNodeName())) {
             //oDestchildNode.getParentNode().removeChild(oDestchildNode);    //Not Working
             //oDoc3.getDocumentElement().removeChild(oDestchildNode); //Not Working
           }
       }
   }   
 }

2 个答案:

答案 0 :(得分:1)

您需要从父节点创建一个单独的引用作为元素,以便您不引用要删除的节点:

File fDestFile = new File("src/myfile.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = null;
    try {
        dBuilder = dbFactory.newDocumentBuilder();
        Document oDoc3 = null;
        oDoc3 = dBuilder.parse(fDestFile);
        NodeList oDestFlowList = oDoc3.getElementsByTagName("NameValuePairs");
        // Loop through all 'NameValuePairs'
        for (int m = oDestFlowList.getLength()-1; m >=0 ; m--) {
            NodeList oDestchildList = oDestFlowList.item(m).getChildNodes();
            // Loop through children of 'NameValuePairs'
            for (int n = oDestchildList.getLength()-1; n >=0 ; n--) {
                // Remove children if they are of the type 'name'
                if(oDestchildList.item(n).getNodeName().equals("name")){
                    oDestFlowList.item(m).removeChild(oDestchildList.item(n));
                    // For debugging
                    System.out.println(oDestchildList.item(n).getNodeName());
                }
            }
        }   
        Source source = new DOMSource(oDoc3);
        Result result = new StreamResult(fDestFile);
        Transformer transformer = null;
        transformer = TransformerFactory.newInstance().newTransformer();
        // Transform your XML document (i.e. save changes to file)
        transformer.transform(source, result);
    } catch (Exception e) {
        // Catch the exception here
        e.printStackTrace();
    }
}

如果您仍然遇到问题,那么我认为这是节点类型的问题。在我为'oDestchildNode.getNodeType()'检查之前,这对我有用,但我会查看你要返回的节点类型并从那里开始。

答案 1 :(得分:1)

以下是最终有效的代码

java -Dtype=text/csv -Durl=http://localhost:8983/solr/jcg/update -jar post.jar books.csv