简体   繁体   中英

How to access a child's child of a parent Node

I have a challenge accessing a child's child of a parentNode. Below is the xml i am working with:

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>";    

Below is the code i have tried:

     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); 
     }

Please how do i get descript, taxAmt and taxCode inside the element tax?

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

This is how I was able to solve the isssue i had: DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource src = new InputSource(); src.setCharacterStream(new StringReader(response)); Document 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();                                                            
            }
         } 


    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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