繁体   English   中英

如何识别在任何属性或子元素中没有值的xml元素?

[英]How to identify xml element with no values in any attributes or sub-elements?

我们正在使用Java中的jaxb动态创建xml。

我们创建许多元素并将其添加到根元素。

假设元素是

a1,a2,a3,...

a1具有子元素以及许多属性。 类似地a2,a3

假设a1在他的任何属性和元素中都没有值,我们根本不应该将元素添加到根中。

当前,我们正在为每个属性检查null,最后决定是否添加该元素。

是否有API可以识别给定元素是否只有空的子元素和属性?

例,

<?xml version="1.0"?>
<catalog>
<book id="bk101">
  <author>Gambardella, Matthew</author>
  <title>XML Developer's Guide</title>
  <genre>Computer</genre>
  <price>44.95</price>
  <publish_date>2000-10-01</publish_date>
  <description>An in-depth look at creating applications 
  with XML.</description>
</book>
<book id="">
  <author></author>
  <title></title>
  <genre></genre>
  <price></price>
  <publish_date></publish_date>
  <description></description>
</book>
<book id="">
  <author>Corets, Eva</author>
  <title></title>
  <genre></genre>
  <price></price>
  <publish_date></publish_date>
  <description></description>
</book>
<book id="bk104">
  <author></author>
  <title></title>
  <genre></genre>
  <price></price>
  <publish_date></publish_date>
  <description></description>
</book>
<book id="">
  <author></author>
  <title></title>
  <genre></genre>
  <price></price>
  <publish_date></publish_date>
  <description>The two daughters of Maeve, half-sisters, 
  battle one another for control of England. Sequel to 
  Oberon's Legacy.</description>
</book>
</catalog>

XML做到了:

String xml=""; // YOUR XML HERE, you can also load if from file

DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder builder = builderFactory.newDocumentBuilder();
// PARSE
Document document = builder.parse(new InputSource(new StringReader(xml)));

// ALL NODES SECOND LEVEL
XPath xPath = XPathFactory.newInstance().newXPath();
String expression="/*/*";

NodeList nodes  = (NodeList)  xPath.compile(expression).evaluate(document, XPathConstants.NODESET);

// Iterate ...
for(int i=0; i<nodes.getLength(); i++)
{
 Node the_node = nodes.item(i);

 if(the_node instanceof Element)
    {
    Element element=(Element) the_node;

    // CONTENT: VOID ?
    String content_trimed=element.getTextContent().trim();

    // NOT THIS !
    if (content_trimed.length()>0) continue;

    // ATTRIBUTES
    boolean to_delete=true; // default

    NamedNodeMap attributes=element.getAttributes();
    for (int k=0;k<attributes.getLength();k++)
        {
        String value=attributes.item(k).getNodeValue();
        if (!value.equals("")) to_delete=false;
        }

    if (!to_delete) continue;

    // OK : DELETE THIS !!!
    Node father=the_node.getParentNode();
    father.removeChild(the_node);
    }

}

// XML => STRING
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(document),new StreamResult(buffer));
String out = buffer.toString();
System.out.println("HIERARCHY"+out);

// STORE IT: EXERCICE

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM