2017-06-05 16 views
0

parentNodeの子ノードにアクセスする際に問題があります。以下は、私が働いているXMLです:親ノードの子ノードにアクセスする方法

 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
    InputSource src = new InputSource();` 
    src.setCharacterStream(new StringReader(response)); 
    Document doc = builder.parse(src); 
    String amount =doc.getElementsByTagName("amount").item(0).getTextContent(); 
    NodeList nodeList = doc.getElementsByTagName("tax"); 
    for (int i = 0; i < nodeList.getLength(); i++) 
    { 
    Node childNode = nodeList.item(i); 
    } 

どのように私は要素税の内側DESCRIPT、taxAmtとtaxCodeを取得するようにしてください:以下

String response = "<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<closure> 
    <amount>1055296.0000</amount> 
    <currency>USD</currency> 
    <unit>2</unit>   
    <year>2012</year> 
    <taxes> 
     <tax> 
      <descript>FEE LEVY</descript>    
      <taxAmt> 
       <amt>30304.0000</amt> 
       <currency>USD</currency>      
      </taxAmt> 
      <taxCode>SUR</taxCode> 
     </tax> 
     <tax> 
      <descript>MED LEVY</descript>    
      <taxAmt> 
       <amt>25125.0000</amt> 
       <currency>USD</currency>      
      </taxAmt> 
      <taxCode>CIS</taxCode> 
     </tax>   
    </taxes> 
</closure>";  

は私がしようとしているコードはありますか?

+0

XMLをインデントしてください。 – Kaushal28

+0

@ Kaushal28 ...私は自分のXMLをうまくインデントしました。 – hyzic23

答えて

0
doc.getElementsByTagName("amount").item(0).getFirstChild().getNodeValue() 
+0

返事ありがとう、私は実際にを取得したいと思います。親切に助けて – hyzic23

0

これは私が私が持っていたisssue解決することができた方法です:。 DocumentBuilderのビルダー= DocumentBuilderFactory.newInstanceを()newDocumentBuilder(); InputSource src = new InputSource(); src.setCharacterStream(新しいStringReader(レスポンス)); ドキュメントdoc = builder.parse(src);

NodeList nodeList = doc.getElementsByTagName("tax"); 
Node node; 

    for (int i = 0; i < nodeList.getLength(); i++) 
    {   
     Node childNode = nodeList.item(i); 
     NodeList nodelist = childNode.getChildNodes(); 

     for(int ii = 0; ii < nodelist.getLength(); ii++) 
     {    
       node = nodelist.item(ii); 
       if(node.getNodeName().equalsIgnoreCase("description")) 
       { 
        node.getTextContent(); 
       }    
       else if(node.getNodeName().equalsIgnoreCase("taxAmount")) 
       { 
        NodeList taxAmountNodelist = node.getChildNodes(); 
        for(int iii = 0; iii < taxAmountNodelist.getLength(); iii++) 
        { 
         Node taxAmountNode = taxAmountNodelist.item(iii); 
         if(taxAmountNode.getNodeName().equalsIgnoreCase("amount")) 
         { 
          taxAmountNode.getTextContent();        
         } 
        } 
       }     
      else if(node.getNodeName().equalsIgnoreCase("taxcode")) 
      {        
       node.getTextContent();                
      } 
     } 


    } 
関連する問題