0
我有一個xml文檔,有多個。我能夠得到和帳戶的詳細信息(,,等我有困難把事情像CARD_TYPE,年,月,first_six等如何從使用JAVA DOM的嵌套XML提取數據?
有此文件中的200筆交易,因此循環。
<transaction href="https://test.com" type="cc">
<source>subscription</source>
<created_at type="datetime">2014-03-06T20:59:03Z</created_at>
<details>
<account>
<account_code>234234234</account_code>
<first_name>asdadad</first_name>
<last_name>asdadasd3433</last_name>
<company nil="nil"></company>
<email>[email protected]</email>
<billing_info type="credit_card">
<first_name>asdasdasd</first_name>
<last_name>asdasdasd23434</last_name>
<address1 nil="nil"></address1>
<address2 nil="nil"></address2>
<city nil="nil"></city>
<state nil="nil"></state>
<zip nil="nil"></zip>
<country nil="nil"></country>
<phone nil="nil"></phone>
<vat_number nil="nil"></vat_number>
<card_type>Visa</card_type>
<year type="integer">2039</year>
<month type="integer">6</month>
<first_six>111111</first_six>
<last_four>9999</last_four>
</billing_info>
</account>
</details>
<a name="refund" href="https://test.com/refund" method="delete"/>
</transaction>
我想我的代碼時出現此錯誤:
java.lang.NullPointerException
at test.test.getTransactions(test.java:288)
at test.test.main(test.java:53)
這裏就是我想:
try {
NodeList nList2 = eElement.getElementsByTagName("details");
Node nNode2 = nList2.item(0);
Element eElement2 = (Element) nNode2;
//get some other info in try catch blocks here (removed for reading)
try {
System.out.println("attempting billing info");
NodeList nList3 = eElement2.getElementsByTagName("billing_info");
Node nNode3 = nList3.item(0);
Element eElement3 = (Element) nNode3;
System.out.println("attempting credit_year");
System.out.println("credit_year: " + eElement3.getElementsByTagName("credit_year").item(0).getTextContent());
} catch (Exception ex) {
ex.printStackTrace();
}
}
這對我很有幫助,非常感謝! – Grundkurs