• <i id='ZGKAp'><tr id='ZGKAp'><dt id='ZGKAp'><q id='ZGKAp'><span id='ZGKAp'><b id='ZGKAp'><form id='ZGKAp'><ins id='ZGKAp'></ins><ul id='ZGKAp'></ul><sub id='ZGKAp'></sub></form><legend id='ZGKAp'></legend><bdo id='ZGKAp'><pre id='ZGKAp'><center id='ZGKAp'></center></pre></bdo></b><th id='ZGKAp'></th></span></q></dt></tr></i><div id='ZGKAp'><tfoot id='ZGKAp'></tfoot><dl id='ZGKAp'><fieldset id='ZGKAp'></fieldset></dl></div>

    1. <legend id='ZGKAp'><style id='ZGKAp'><dir id='ZGKAp'><q id='ZGKAp'></q></dir></style></legend>

        <bdo id='ZGKAp'></bdo><ul id='ZGKAp'></ul>
      <tfoot id='ZGKAp'></tfoot>

        <small id='ZGKAp'></small><noframes id='ZGKAp'>

      1. Java 删除空的 XML 标记

        Java Remove empty XML tags(Java 删除空的 XML 标记)
      2. <tfoot id='Sbjsq'></tfoot>

          • <bdo id='Sbjsq'></bdo><ul id='Sbjsq'></ul>

                <i id='Sbjsq'><tr id='Sbjsq'><dt id='Sbjsq'><q id='Sbjsq'><span id='Sbjsq'><b id='Sbjsq'><form id='Sbjsq'><ins id='Sbjsq'></ins><ul id='Sbjsq'></ul><sub id='Sbjsq'></sub></form><legend id='Sbjsq'></legend><bdo id='Sbjsq'><pre id='Sbjsq'><center id='Sbjsq'></center></pre></bdo></b><th id='Sbjsq'></th></span></q></dt></tr></i><div id='Sbjsq'><tfoot id='Sbjsq'></tfoot><dl id='Sbjsq'><fieldset id='Sbjsq'></fieldset></dl></div>

                  <small id='Sbjsq'></small><noframes id='Sbjsq'>

                  <legend id='Sbjsq'><style id='Sbjsq'><dir id='Sbjsq'><q id='Sbjsq'></q></dir></style></legend>
                    <tbody id='Sbjsq'></tbody>

                • 本文介绍了Java 删除空的 XML 标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在寻找一个简单的 Java 片段来从(任何)XML 结构中删除空标签

                  I'm looking for a simple Java snippet to remove empty tags from a (any) XML structure

                  <xml>
                      <field1>bla</field1>
                      <field2></field2>
                      <field3/>
                      <structure1>
                         <field4>bla</field4>
                         <field5></field5>
                      </structure1>
                  </xml>
                  

                  应该变成;

                  <xml>
                      <field1>bla</field1>
                      <structure1>
                         <field4>bla</field4>
                      </structure1>
                  </xml>
                  

                  推荐答案

                  我想知道使用 XOM 库并试一试.

                  I was wondering whether it would be easy to do this with the XOM library and gave it a try.

                  结果很简单:

                  import nu.xom.*;
                  
                  import java.io.File;
                  import java.io.IOException;
                  
                  public class RemoveEmptyTags {
                  
                      public static void main(String[] args) throws IOException, ParsingException {
                          Document document = new Builder().build(new File("original.xml"));
                          handleNode(document.getRootElement());
                          System.out.println(document.toXML()); // empty elements now removed
                      }
                  
                      private static void handleNode(Node node) {
                          if (node.getChildCount() == 0 && "".equals(node.getValue())) {
                              node.getParent().removeChild(node);
                              return;
                          }
                          // recurse the children
                          for (int i = 0; i < node.getChildCount(); i++) { 
                              handleNode(node.getChild(i));
                          }
                      }
                  }
                  

                  这可能无法正确处理所有极端情况,例如完全空的文档.以及如何处理原本为空但具有属性的元素?

                  This probably won't handle all corner cases properly, like a completely empty document. And what to do about elements that are otherwise empty but have attributes?

                  如果要保存带有属性的 XML 标签,我们可以在方法 'handleNode' 中添加以下检查:

                  If you want to save XML tags with attributes, we can add in the method 'handleNode' the following check:

                  ... && ((Element) node).getAttributeCount() == 0) )
                  

                  另外,如果xml有两个或多个空标签,一个接一个;这种递归方法不会删除所有空标签!

                  Also, if the xml has two or more empty tags, one after another; this recursive method doesn't remove all empty tags!

                  (这个答案是我对 XOM 作为潜在的评估的一部分 替换为 dom4j.)

                  (This answer is part of my evaluation of XOM as a potential replacement to dom4j.)

                  这篇关于Java 删除空的 XML 标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                    <legend id='dQkRj'><style id='dQkRj'><dir id='dQkRj'><q id='dQkRj'></q></dir></style></legend>

                  1. <tfoot id='dQkRj'></tfoot>
                      <tbody id='dQkRj'></tbody>

                        <bdo id='dQkRj'></bdo><ul id='dQkRj'></ul>

                          <small id='dQkRj'></small><noframes id='dQkRj'>

                          <i id='dQkRj'><tr id='dQkRj'><dt id='dQkRj'><q id='dQkRj'><span id='dQkRj'><b id='dQkRj'><form id='dQkRj'><ins id='dQkRj'></ins><ul id='dQkRj'></ul><sub id='dQkRj'></sub></form><legend id='dQkRj'></legend><bdo id='dQkRj'><pre id='dQkRj'><center id='dQkRj'></center></pre></bdo></b><th id='dQkRj'></th></span></q></dt></tr></i><div id='dQkRj'><tfoot id='dQkRj'></tfoot><dl id='dQkRj'><fieldset id='dQkRj'></fieldset></dl></div>