java - 无法删除 XML 中的特定节点

标签 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
           }
       }
   }   
 }

最佳答案

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

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()”之前,这对我有用,但我会查看您返回的节点类型并从那里开始。

关于java - 无法删除 XML 中的特定节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37308045/

相关文章:

android - 如何将矢量路径转换为图像以及反之亦然

c# - 帮助我了解单元测试中此 FAIL 发生了什么

java - 我在解密使用 RSA 生成的公钥 (.jks) 编码的 128 位 AES key 时遇到 BadPaddingException

java - 将注释(XFDF 格式)合并到 PDF 中

C# : the close method of Xml. 加载(文件)

java - 如何生成包含已解析实体的 XML 文档的*精确*副本

java - 如何扩充AVL树?

java - fragment 消失

java - 如何使用 getValue(Subclass.class) 在 Firebase 中反序列化子类

xml - 使用 xslt 删除信息路径 XML 中的信息路径声明? <? mso ......>