【发布时间】:2017-02-03 16:25:43
【问题描述】:
Java 代码
XPathExpression readOcc = xpath.compile("//flexTM/attrGroupMany[contains(@name,'allergenRelatedInformation')]");
Object rObj = (Object) readOcc.evaluate(doc,XPathConstants.NODESET);
NodeList agm = (NodeList) rObj;
System.out.println("" + agm.getLength());
for (int i=0; i<agm.getLength(); i++){
Element element = (Element) agm.item(i).getChildNodes();
NodeList row = element.getElementsByTagName("row");
System.out.println("row len " + row.getLength());
for(int j=0;j<row.getLength(); j++){
Element eAttr = (Element) row.item(j);
System.out.println(eAttr.getNodeName());
NodeList attr = eAttr.getElementsByTagName("attrGroupMany");
for (int k=0;k<attr.getLength();k++){
Element eAgm = (Element) attr.item(k);
System.out.println(eAgm.getNodeName());
NodeList iattr = eAgm.getChildNodes();
System.out.println(iattr.getLength());
System.out.println(iattr.item(1).getNodeValue());
//NodeList iattr = eAgm.getElementsByTagName("row");
for(int l=0;i<iattr.getLength();l++){
Element iAttr = (Element) iattr.item(l);
System.out.println(iAttr.getNodeName());
//System.out.println(iAttr.getNodeValue());
}
}
}
XML
<item>
<attrGroupMany name="manufacturer">
<row>
<attr name="gln">7689</attr>
<attr name="name">XYZ Inc</attr>
</row>
</attrGroupMany>
<attrGroupMany name="allergenRelatedInformation">
<row>
<attr name="allergenSpecificationAgency">FDA</attr>
<attr name="allergenSpecificationName">BIG 8</attr>
<attrGroupMany name="allergen">
<row>
<attr name="allergenTypeCode">AC</attr>
<attr name="levelOfContainmentCode">FREE_FROM</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="allergenSpecificationAgency">FDA</attr>
<attr name="allergenSpecificationName">BIG 8</attr>
<attrGroupMany name="allergen">
<row>
<attr name="allergenTypeCode">AE</attr>
<attr name="levelOfContainmentCode">FREE_FROM</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="allergenSpecificationAgency">FDA</attr>
<attr name="allergenSpecificationName">BIG 8</attr>
<attrGroupMany name="allergen">
<row>
<attr name="allergenTypeCode">AF</attr>
<attr name="levelOfContainmentCode">FREE_FROM</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="allergenSpecificationAgency">FDA</attr>
<attr name="allergenSpecificationName">BIG 8</attr>
<attrGroupMany name="allergen">
<row>
<attr name="allergenTypeCode">AM</attr>
<attr name="levelOfContainmentCode">FREE_FROM</attr>
</row>
</attrGroupMany>
</row>
</attrGroupMany>
</item>
<item>
<attrGroupMany name="manufacturer">
<row>
<attr name="gln">7689</attr>
<attr name="name">XYZ Inc</attr>
</row>
</attrGroupMany>
<attrGroupMany name="allergenRelatedInformation">
<row>
<attr name="allergenSpecificationAgency">FDA</attr>
<attr name="allergenSpecificationName">BIG 8</attr>
<attrGroupMany name="allergen">
<row>
<attr name="allergenTypeCode">AC</attr>
<attr name="levelOfContainmentCode">FREE_FROM</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="allergenSpecificationAgency">FDA</attr>
<attr name="allergenSpecificationName">BIG 8</attr>
<attrGroupMany name="allergen">
<row>
<attr name="allergenTypeCode">AE</attr>
<attr name="levelOfContainmentCode">FREE_FROM</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="allergenSpecificationAgency">FDA</attr>
<attr name="allergenSpecificationName">BIG 8</attr>
<attrGroupMany name="allergen">
<row>
<attr name="allergenTypeCode">AF</attr>
<attr name="levelOfContainmentCode">FREE_FROM</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="allergenSpecificationAgency">FDA</attr>
<attr name="allergenSpecificationName">BIG 8</attr>
<attrGroupMany name="allergen">
<row>
<attr name="allergenTypeCode">AM</attr>
<attr name="levelOfContainmentCode">FREE_FROM</attr>
</row>
</attrGroupMany>
</row>
</attrGroupMany>
</item>
在上面的 XML 中,有 2 个项目标签,每个标签都有自己的节点 attrGroupMany,属性为 allergenRelatedInformation。我正在尝试解析每个级别的 xml,以便我可以打印父节点和子节点的所有值。不确定我上面的代码有什么问题,它失败了。
【问题讨论】:
-
您应该考虑递归执行此操作。
-
“失败了” 描述不充分。请访问help center 并阅读How to Ask 以了解如何有效地使用本网站。
-
我有理由不递归循环。有人可以指出我的代码有什么问题吗?谢谢
-
永远不要告诉我们某事“失败”或“不工作”而不告诉我们它是如何失败的。
-
for(int j=0;j
下打印行元素的数量。但看起来它正在考虑 下的偶数行。