Java 如何识别在任何属性或子元素中没有值的xml元素?
我们正在java中使用jaxb动态创建xml 我们创建并向根元素添加许多元素 假设元素是 a1, a2, a3, a1具有子元素和许多属性。同样,a2、a3 假设a1的任何属性和元素中都没有值,我们就不应该将该元素添加到根中 目前,我们正在检查每个属性是否为null,并最终决定是否添加该元素 是否有API来标识给定元素是否只有空的子元素和属性 例如Java 如何识别在任何属性或子元素中没有值的xml元素?,java,xml,Java,Xml,我们正在java中使用jaxb动态创建xml 我们创建并向根元素添加许多元素 假设元素是 a1, a2, a3, a1具有子元素和许多属性。同样,a2、a3 假设a1的任何属性和元素中都没有值,我们就不应该将该元素添加到根中 目前,我们正在检查每个属性是否为null,并最终决定是否添加该元素 是否有API来标识给定元素是否只有空的子元素和属性 例如 <?xml version="1.0"?> <catalog> <book id="bk101"> <
<?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开发人员指南
电脑类
44.95
2000-10-01
深入了解如何创建应用程序
使用XML。
科雷茨,伊娃
梅芙的两个女儿,同父异母的姐妹,
为争夺英国的控制权而互相争斗。续集
奥伯伦的遗产。
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
stringxml=”“;//在这里,您还可以从文件中加载XML
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
setNamespaceAware(true);
DocumentBuilder=builderFactory.newDocumentBuilder();
//解析
documentdocument=builder.parse(新的InputSource(新的StringReader(xml));
//第二级所有节点
XPath=XPathFactory.newInstance().newXPath();
字符串表达式=“/*/*”;
NodeList节点=(NodeList)xPath.compile(expression).evaluate(document,xpathcontents.NODESET);
//迭代。。。
对于(int i=0;i0)continue;
//属性
boolean to_delete=true;//违约
NamedNodeMap attributes=element.getAttributes();
for(int k=0;k字符串
TransformerFactory transFactory=TransformerFactory.newInstance();
变压器=transFactory.newTransformer();
StringWriter缓冲区=新StringWriter();
setOutputProperty(OutputKeys.OMIT_XML_声明,“yes”);
transform(新的DOMSource(文档)、新的StreamResult(缓冲区));
String out=buffer.toString();
System.out.println(“层次结构”+out);
//储存:EXERCICE
将帮助您显示示例XML而不是仅显示一个标识符列表。请仔细查看上面的XMLSO,现在您已经给了我们示例XML,但没有告诉我们您希望挑选哪些元素。我假设是<代码>作者>代码>后面的那些?作为属性值的空字符串是什么?您用哪一个API来表示?se XML?(有很多可用的…)你能发布一个简短但完整的程序来演示你到目前为止得到的代码,以及哪里出错了吗?在上面的例子中,我想跳过第二个元素,因为它在元素和属性中都没有任何值。所以我需要在任何java第三方库中使用一个方法或API来验证。你不应该期望有一个me这正是你想要的。你需要自己编写这个方法——你试过了吗?它看起来像什么?出了什么问题?