如何使用JAVA DOM从嵌套XML中提取数据? [英] How do I extract data from nested XML using JAVA DOM?

查看:40
本文介绍了如何使用JAVA DOM从嵌套XML中提取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有多个的xml文档.我可以获取和帐户详细信息(等).我很难获取诸如card_type,年,月,first_six等信息.

I have an xml document that has multiple . I am able to get the and account details (,, etc. I am having difficulty getting things like card_type, year, month, first_six, etc.

此文档中有200个事务,因此是循环.

There are 200 transactions within this document, hence the loop.

  <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>test@gmail.com</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>

尝试我的代码时出现此错误:

I get this error when trying my code:

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

}

推荐答案

以下是一些代码,可以指导您使用DOM解析XML文件.您缺少文档构建器.

Here is some code that should guide you on using DOM to parse XML files. You were missing the Document builders.

    //Build the document from the xmlString
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(new InputSource(new StringReader(xmlString)));

    //Get all the transaction elements and then loop over them
    NodeList transaction = doc.getElementsByTagName("transaction");
    for(int j = 0; j < transaction.getLength(); j++) {
        //Traverse down the transaction node till we get the billing info
        NodeList details = ((Element)transaction.item(j)).getElementsByTagName("details");
        NodeList account = ((Element)details.item(0)).getElementsByTagName("account");
        NodeList billinginfo = ((Element)account.item(0)).getElementsByTagName("billing_info");

        System.out.println("===Billing Info===");
        System.out.println("Type: "+((Element)billinginfo.item(0)).getAttribute("type"));

        //Get all children nodes from billing info
        NodeList billingChildren = billinginfo.item(0).getChildNodes();

        for(int i = 0; i < billingChildren.getLength(); i++) {
            Node current = billingChildren.item(i);
            //Only want stuff from ELEMENT nodes
            if(current.getNodeType() == Node.ELEMENT_NODE) {
                System.out.println(current.getNodeName()+": "+current.getTextContent());
            }
        }
    }

这将从您的示例中产生以下内容.

This produces the following from your example.

===Billing Info===
Type: credit_card
first_name: asdasdasd
last_name: asdasdasd23434
address1:
address2:
city:
state:
zip:
country:
phone:
vat_number:
card_type: Visa
year: 2039
month: 6
first_six: 111111
last_four: 9999

这篇关于如何使用JAVA DOM从嵌套XML中提取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆