移除父节点 DomParser Java

Posted

技术标签:

【中文标题】移除父节点 DomParser Java【英文标题】:Removing parent node DomParser Java 【发布时间】:2021-06-21 14:40:46 【问题描述】:

我有一个如下所示的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<Start xmlns="http://soap.sforce.com/2006/04/metadata">
<applicationVisibilities>
    <application>Foo1</application>
    <default>false</default>
    <visible>false</visible>
</applicationVisibilities>
<applicationVisibilities>
    <application>Foo2</application>
    <default>false</default>
    <visible>false</visible>
</applicationVisibilities>
<applicationVisibilities>
    <application>Foo3</application>
    <default>false</default>
    <visible>false</visible>
</applicationVisibilities>
</Start>

基于与 applicationKey 相关的一些逻辑,我想删除以下 XML。我怎样才能做到这一点 ?目前,我可以删除子节点,但无法删除 applicationVisibilities 节点及其仅与 FOO2 相关的所有子节点

<applicationVisibilities>
    <application>Foo2</application>
    <default>false</default>
    <visible>false</visible>
</applicationVisibilities>

Java 代码:

final NodeList origNodeList = document.getElementsByTagName("applicationVisibilities");
    for (int temp = 0; temp < origNodeList.getLength(); temp++) 
        Node nNode = origNodeList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) 
            Element eElement = (Element) nNode;
            final String application = eElement.getElementsByTagName("application").item(0).getTextContent();
            if(application.equals("Foo2")) 
                Node node = eElement.getElementsByTagName("application").item(0);
                if (node != null) 
                    if (node.getNodeName().equals("application")) 
                        node.getParentNode().removeChild(node);
                        node.normalize();
                        eElement.normalize();
                    

                
            
        
    

【问题讨论】:

为什么这个标签是xslt?您在寻找 XSLT 解决方案吗? 【参考方案1】:

在下面的解决方案中,我转换了xml string to classes。然后从 Start 类的列表中删除指定的应用程序。最后通过jackson-dataformat-xml将对象转换成xml字符串。

我使用了以下 maven 依赖项:

<dependency>
       <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-xml</artifactId>
      <version>2.11.1</version>
</dependency>
String xml =
        """
        <?xml version="1.0" encoding="UTF-8"?>
        <Start xmlns="http://soap.sforce.com/2006/04/metadata">
            <applicationVisibilities>
                <application>Foo1</application>
                <default>false</default>
                <visible>false</visible>
            </applicationVisibilities>
            <applicationVisibilities>
                <application>Foo2</application>
                <default>false</default>
                <visible>false</visible>
            </applicationVisibilities>
            <applicationVisibilities>
                <application>Foo3</application>
                <default>false</default>
                <visible>false</visible>
            </applicationVisibilities>
        </Start>
        """;
public static void main(String[] args) 
        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
        Start start = null;
        try 
            start = xmlMapper.readValue(xml, Start.class);
            List<applicationVisibilities> tempApplicationVisibilities = start.applicationVisibilities;
            for (applicationVisibilities visibility : tempApplicationVisibilities) 
                if(visibility.application.equals("Foo2")) 
                    start.applicationVisibilities.remove(visibility);
                    String updatedXml = xmlMapper.writeValueAsString(start);
                    System.out.println(updatedXml);
                
            
         catch (JsonProcessingException e) 
            e.printStackTrace();
        

public static class applicationVisibilities 
    public String application;

    @JacksonXmlProperty(localName = "default")
    public boolean _default;

    public boolean visible;


public static class Start 
    @JacksonXmlProperty(localName = "applicationVisibilities")
    @JacksonXmlElementWrapper(useWrapping = false)
    public List<applicationVisibilities> applicationVisibilities;

输出:

<Start>
  <applicationVisibilities>
    <application>Foo1</application>
    <visible>false</visible>
    <default>false</default>
  </applicationVisibilities>
  <applicationVisibilities>
    <application>Foo3</application>
    <visible>false</visible>
    <default>false</default>
  </applicationVisibilities>
</Start>

【讨论】:

以上是关于移除父节点 DomParser Java的主要内容,如果未能解决你的问题,请参考以下文章

怎样移除当前节点

Tree View控件(添加,移除,设置图标)

Codeforces 982 C. Cut 'em all!(dfs)

js移除某一类的div

移除元素

如何在Swift中正确地从父节点中删除SKSpriteNode的子类(要从数组中移除,屏幕上的空格......)?