【问题标题】:Java DOM Parse and Delete NodeJava DOM 解析和删除节点
【发布时间】:2017-02-02 21:48:00
【问题描述】:

在下面的 XML 中,我需要搜索标签 attrGroupMany name="allergenRelatedInformation"删除所有子节点 () 除了它下面的第一个节点

不确定 XSLT/Java DOM 是否是实现这一目标的最佳方式。请帮助。

 XPathExpression delExpr = xpath.compile("//flexTM/attrGroupMany[starts-with(@name,'allergenRelatedInformation')]/row");
 Object obj = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList row = (NodeList) obj;

 for(int i = 1; i < row.getLength(); i++)

        {

            Node attr = row.item(i);

            Element e = (Element) attr;

            System.out.println(row.item(i).getParentNode().getNodeName());

            row.item(1).getParentNode().removeChild(e);
            doc.normalize();
            i-- }

      <attrGroupMany name="manufacturer">
                  <row>
                     <attr name="gln">123456</attr>
                     <attr name="name">ABC 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>
             

【问题讨论】:

    标签: java xslt dom xml-parsing xslt-2.0


    【解决方案1】:

    如果您希望 xml 中的输出不需要进行进一步处理,那么 XSLT 是最佳选择。否则,JDOM 是解析您正在寻找的节点并在稍后在同一 java 代码中执行所需业务逻辑的选项。

    【讨论】:

    • 您能告诉我如何在我的案例中使用 JDOM 进行导航吗?非常感谢。
    • 您希望程序的输出 xml 是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 2012-07-20
    • 2012-06-30
    • 2021-05-30
    • 2016-06-23
    • 2012-07-29
    相关资源
    最近更新 更多