简体   繁体   English

如何解析此XML文件

[英]How to parse this XML file

I am new to programming in java and i have just learned how to parse an xml file. 我是Java编程的新手,我刚刚学习了如何解析xml文件。 But i am not getting any idea on how to parse this xml file. 但是我对如何解析此xml文件一无所知。 Please help me with a code on how to get the tags day1 and their inner tags order1,order2 请提供有关如何获取标签day1及其内部标签order1,order2的代码的帮助我

<RoutePlan>
<day1>
    <Order1>
    <customer> XYZ</customer>
    <address> INDIA </address>
    <data> 10-10-2011 </data>
    <time> 9.30 A.M </time>
    </Order1>

    <Order2>
    <customer> ABC </customer>
    <address> US </address>
    <data> 10-10-2011 </data>
    <time> 10.30 A.M </time>
    </Order2>
</day1>

I wrote the following code to retrieve. 我编写了以下代码进行检索。 But i am only getting the data in order1 but not in order2 但是我只在order1中获取数据,而不是在order2中获取数据

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document document = db.parse(file);
      document.getDocumentElement().normalize();
      System.out.println("Root Element: "+document.getDocumentElement().getNodeName());
      NodeList node =  document.getElementsByTagName("day1");

      for(int i=0;i<node.getLength();i++){
          Node firstNode = node.item(i);
          Element element = (Element) firstNode;
          NodeList customer = element.getElementsByTagName("customer");
          Element customerElement = (Element) customer.item(0);
          NodeList firstName = customerElement.getChildNodes();
          System.out.println("Name: "+((firstName.item(0).getNodeValue())));

          NodeList address = element.getElementsByTagName("address");
          Element customerAddress = (Element) address.item(0);
          NodeList addName = customerAddress.getChildNodes();
          System.out.println("Address: "+((addName.item(0).getNodeValue())));

          NodeList date = element.getElementsByTagName("date");
          Element customerdate = (Element) date.item(0);
          NodeList dateN = customerdate.getChildNodes();
          System.out.println("Address: "+((dateN.item(0).getNodeValue())));


          NodeList time = element.getElementsByTagName("time");
          Element customertime = (Element) time.item(0);
          NodeList Ntime = customertime.getChildNodes();
          System.out.println("Time: "+((Ntime.item(0).getNodeValue())));
      }

I can give you not one, not two, but three directions to parse this XML (there are more but let's say they are the most commons ones): 我可以给您的不是一个,不是两个,而是三个方向来解析此XML(还有更多的方法,但可以说它们是最常见的方法):

  • DOM -> two good resources to start : here and here DOM->两个好资源开始: 这里这里
  • SAX -> quickstart from official website: here SAX->官方网站快速入门: 此处
  • StAX -> a good introduction: here StAX->一个很好的介绍: 在这里

Judging by the size of your XML document, I'd probably go for a DOM parsing, which gonna be the easiest to implement and to use (but if you have to deal with larger files, take a look at SAX for reading-only manipulations and StAX for reading and writing ones). 从XML文档的大小来看,我可能会去进行DOM解析,这将是最容易实现和使用的(但是,如果您必须处理更大的文件,请看一下SAX的只读操作)和StAX进行读写)。

The reason you are getting only "Order1" elements is because: 您仅获得“ Order1”元素的原因是:

  • You lock on the "day1" node. 您锁定“ day1”节点。
  • You retrieve the "customer" elements by tag name which returns 2 elements. 通过标记名称检索“客户”元素,该元素返回2个元素。
  • You retrieve the first element and print its value and hence the second "customer" is ignored. 您检索第一个元素并打印其值,因此第二个“客户”将被忽略。

When working with DOM, be prepared to spin up multiple loops for retrieving data. 使用DOM时,请准备好启动多个循环以检索数据。 Also, you are a bit misguided when it comes to representing your schema. 另外,在表示架构时,您也会有些误解。 You really don't need to name "elements" as "day1"/"order1" etc. In XML, that can be simply expressed by having multiple "day" or "order" elements which in turn automatically enforces ordering. 您确实不需要将“ elements”命名为“ day1” /“ order1”等。在XML中,可以简单地通过具有多个“ day”或“ order”元素来表示,这些元素又会自动执行排序。 An example XML would look like: XML示例如下所示:

<route-plan>
    <day>
        <order>
            <something>
        </order>
    </day>
    <day>
        <order>
            <something>
        </order>
    </day>
</route-plan>

Now retrieving "day" elements is a simple matter of: 现在,检索“ day”元素很简单:

  • Look up "day" elements by tag name 通过标签名称查找“天”元素
  • For each "day" element 对于每个“天”元素
    • Look up "order" element by tag name 通过标签名称查找“订单”元素
    • For each "order" element 对于每个“订单”元素
      • Print out the value of "customer"/"address" etc. 打印出“客户” /“地址”等值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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