訪問孩子的父節點的孩子有一個挑戰。如何訪問父節點的孩子的孩子節點
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>";
下面是代碼我曾嘗試::下面是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元素稅裏面?
請縮進您的XML。 – Kaushal28
@ Kaushal28 ...我已經縮進了我的xml。 – hyzic23