简体   繁体   中英

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

We are dynamically creating xml using jaxb in java.

We create and add numerous elements to the root element.

Assume the elements are

a1, a2, a3, ...

a1 has sub-elements as well as many attributes. similarly a2, a3

assume if a1 has no values in any of his attributes and elements we should not add the element at all to the root.

currently we are checking for null for each and every attribute and finally deciding whether to add that element or not.

Is there an API to identify whether the given element has only empty sub-elements and attributes?

Example,

<?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 does it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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