2017-02-02 7 views
0

を削除します。JavaのDOM解析し、それ</strong>下の最初のものを除いて、私はタグ<strong>attrGroupMany名=「allergenRelatedInformation」</strong>を検索し、<strong>すべての子ノードを(削除する必要があり、以下のXML)内のノード

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> 
 
     

答えて

0

あなたはさらに処理を行うために必要ではないXMLで出力を期待していた場合、XSLTは、最良の選択肢です。それ以外の場合、JDOMは、探しているノードを解析し、後で同じJavaコードを指すように必要なビジネスロジックを実行するオプションです。

+0

私の場合、JDOMを使用してナビゲートする方法を教えてください。どうもありがとう。 – user3092856

+0

あなたのプログラムから期待している出力XMLは何ですか? – ssanrao

関連する問題