I am new to programming in java and i have just learned how to parse an xml file. But i am not getting any idea on how to parse this xml file. Please help me with a code on how to get the tags day1 and their inner tags order1,order2
我是java编程的新手,我刚学会了如何解析xml文件。但我不知道如何解析这个xml文件。请帮我一个代码,了解如何获取标签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
我编写了以下代码来检索。但我只是按顺序获取数据,而不是顺序2
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())));
}
2 个解决方案
#1
4
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
- SAX -> quickstart from official website: here
- StAX -> a good introduction: here
DOM - >两个很好的资源:这里和这里
SAX - >来自官方网站的快速入门:这里
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用于读写的)。
#2
0
The reason you are getting only "Order1" elements is because:
您只获得“Order1”元素的原因是:
- You lock on the "day1" node.
- You retrieve the "customer" elements by tag name which returns 2 elements.
- You retrieve the first element and print its value and hence the second "customer" is ignored.
您锁定“day1”节点。
您可以通过标记名称检索“customer”元素,该元素返回2个元素。
您检索第一个元素并打印其值,因此忽略第二个“客户”。
When working with DOM, be prepared to spin up multiple loops for retrieving data. 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. An example XML would look like:
使用DOM时,请准备好旋转多个循环以检索数据。此外,在表示模式时,您有点误导。您实际上不需要将“元素”命名为“day1”/“order1”等。在XML中,可以通过具有多个“day”或“order”元素来简单地表达,这些元素反过来自动强制执行排序。示例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.
打印出“客户”/“地址”等的值。
按标签名称查找“order”元素
对于每个“订单”元素打印出“客户”/“地址”等的值。
按标签名称查找“day”元素
对于每个“day”元素按标签名称查找“order”元素对于每个“order”元素打印出“customer”/“address”等的值。
#1
4
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
- SAX -> quickstart from official website: here
- StAX -> a good introduction: here
DOM - >两个很好的资源:这里和这里
SAX - >来自官方网站的快速入门:这里
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用于读写的)。
#2
0
The reason you are getting only "Order1" elements is because:
您只获得“Order1”元素的原因是:
- You lock on the "day1" node.
- You retrieve the "customer" elements by tag name which returns 2 elements.
- You retrieve the first element and print its value and hence the second "customer" is ignored.
您锁定“day1”节点。
您可以通过标记名称检索“customer”元素,该元素返回2个元素。
您检索第一个元素并打印其值,因此忽略第二个“客户”。
When working with DOM, be prepared to spin up multiple loops for retrieving data. 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. An example XML would look like:
使用DOM时,请准备好旋转多个循环以检索数据。此外,在表示模式时,您有点误导。您实际上不需要将“元素”命名为“day1”/“order1”等。在XML中,可以通过具有多个“day”或“order”元素来简单地表达,这些元素反过来自动强制执行排序。示例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.
打印出“客户”/“地址”等的值。
按标签名称查找“order”元素
对于每个“订单”元素打印出“客户”/“地址”等的值。
按标签名称查找“day”元素
对于每个“day”元素按标签名称查找“order”元素对于每个“order”元素打印出“customer”/“address”等的值。