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

时间:2015-11-27 07:00:42

标签: java xml

我们在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>

1 个答案:

答案 0 :(得分:0)

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