繁体   English   中英

Java DOM解析不适用于深层xml结构

[英]Java DOM parsing not working for deep xml structure

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,以便可以打印父节点和子节点的所有值。 不知道我上面的代码有什么问题,它失败了。

我建议您避免直接使用org.w3c.dom.*类,因为最终的代码可能很难阅读和维护。 您可以编写一个类层次结构,然后使用JAXB将xml插入其中,这是常规的Java方法。

或者,如果您想更直接地使用它,但是以一种不错的方式使用它,可以使用Dynamics库。

让我给你一个使用这个例子。 假设xml有效,即具有单个父元素,则称其为“数据”。 您的文档如下所示。

<data>
  <item>
    <!-- contents as you specified -->
  </item>
  <item>
    <!-- contents as you specified -->
  </item>
</data>

XmlDynamic实例将使您以直接的空安全方式遍历结构,但具有相同的功能和直接性。

让我们获取第二项的第一个“ attr”名称属性

XmlDynamic allergenInfo = new XmlDynamic(xmlStringOrReaderOrInputSourceEtc);

String firstAttrName = allergenInfo
    .get("data|item[1]|attrGroupMany|row|attr|@name")
    .asString(); // gln

或者浏览整个文档并打印属性名称和值

allergenInfo.allChildren()
    .filter(hasElementName("attr")) // import static alexh.weak.XmlDynamic.hasElementName
    .filter(attr -> attr.get("@name").isPresent())
    .forEach(attr -> System.out.println(attr.get("@name").asString() + " -> " + attr.asString()));
// prints all attr names -> values

这是一个单一且轻量级的额外依赖项,即在maven中:

<dependency>
  <groupId>com.github.alexheretic</groupId>
  <artifactId>dynamics</artifactId>
  <version>4.0</version>
</dependency>

查看更多示例https://github.com/alexheretic/dynamics#xml-dynamics

暂无
暂无

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

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