[英]how is read/parsing xml java STAX (mechanism)
I try to understand how is working the mechanism of STAX java. 我尝试了解STAX java的工作原理。
I have this xml file 我有这个xml文件
<?xml version="1.0" encoding="UTF-8"?>
<orders>
<order created='2012-07-12T15:29:33.000' ID='2343'>
<product>
<description>Sony 54.6" (Diag) Xbr Hx929 Internet Tv</description>
<gtin>00027242816657</gtin>
<price currency="USD">2999.99</price>
<supplier>Sony</supplier>
</product>
<product>
<description>Apple iPad 2 with Wi-Fi 16GB - iOS 5 - Black</description>
<gtin>00885909464517</gtin>
<price currency="USD">399.0</price>
<supplier>Apple</supplier>
</product>
<product>
<description>Sony NWZ-E464 8GB E Series Walkman Video MP3 Player Blue</description>
<gtin>00027242831438</gtin>
<price currency="USD">91.99</price>
<supplier>Sony</supplier>
</product>
</order>
<order created='2012-07-13T16:02:22.000' ID='2344'>
<product>
<description>Apple MacBook Air A 11.6" Mac OS X v10.7 Lion MacBook</description>
<gtin>00885909464043</gtin>
<price currency="USD">1149.0</price>
<supplier>Apple</supplier>
</product>
<product>
<description>Panasonic TC-L47E50 47" Smart TV Viera E50 Series LED HDTV</description>
<gtin>00885170076471</gtin>
<price currency="USD">999.99</price>
<supplier>Panasonic</supplier>
</product>
</order>
</orders>
To mimic the behavior of this XML file we created an object with similar attributes 为了模仿此XML文件的行为,我们创建了具有类似属性的对象
public class Product {
private int orderID;
private String createTime;
private String description;
private String gtin;
private String price;
private String supplier;
//getter and setter
}
With this I tried to read my xml file: 我试图以此读取我的xml文件:
if (xmlEvent.isStartElement()){
StartElement startElement = xmlEvent.asStartElement();
if(startElement.getName().getLocalPart().equals("order")){
prod = new Product();
Attribute idAttr = startElement.getAttributeByName(new QName("ID"));
if(idAttr != null){
prod.setgetOrderID(Integer.parseInt(idAttr.getValue()));
}
Attribute orderTime = startElement.getAttributeByName(new QName("created"));
if(orderTime != null){
prod.setgetCreateTime(orderTime.getValue());
}
counter++;
//System.out.println("Obiect creat");
System.out.println(counter);
}
//set the other varibles from xml elements
else if(startElement.getName().getLocalPart().equals("description")){
xmlEvent = xmlEventReader.nextEvent();
prod.setDescription(xmlEvent.asCharacters().getData());
}else if(startElement.getName().getLocalPart().equals("gtin")){
xmlEvent = xmlEventReader.nextEvent();
prod.setGtin(xmlEvent.asCharacters().getData());
}else if(startElement.getName().getLocalPart().equals("price")){
xmlEvent = xmlEventReader.nextEvent();
prod.setPrice(xmlEvent.asCharacters().getData());
}else if(startElement.getName().getLocalPart().equals("supplier")){
xmlEvent = xmlEventReader.nextEvent();
prod.setSupplier(xmlEvent.asCharacters().getData());
}
My problem is that, they are 5 products, but when I try to output them, are not the correct number. 我的问题是,它们是5种产品,但是当我尝试输出它们时,它们不是正确的数字。 1. If in: if(startElement.getName().getLocalPart().equals("orders")) the last parameter is "oders" in the output i see just one object (Panasonic TC-L47E50 47" Smart TV Viera E50 Series LED HDTV) 1.如果在:if(startElement.getName()。getLocalPart()。equals(“ orders”))中,最后一个参数是输出中的“ oders”,那么我只会看到一个对象(Panasonic TC-L47E50 47“ Smart TV Viera E50系列LED HDTV)
What modification i need to do, for read entire information. 我需要做些修改,以读取全部信息。 My scope is to read the attribute "created" and "id" of order but with all my objects, not 1 or 2 Thanks ! 我的范围是读取属性“ created”和“ id”的顺序,但要读取所有对象,而不是1或2。谢谢!
If you're using JaxB
, you don't neet to create classes either even parse XML file by yourself, that's why JaxB is made for! 如果您使用的是JaxB
,那么您甚至都不需要自己创建类,甚至不用自己解析XML文件, 这就是JaxB是为之制造的! :) :)
Basic steps to read-write xml using JaxB
/ Unmarshaller
and XSD
使用JaxB
/ Unmarshaller
和XSD
读写xml的基本步骤
XSD
file of your XML
structure. 创建您的XML
结构的有效 XSD
文件。 XSD
file and auto-generate JAXB classes . 右键单击XSD
文件并自动生成JAXB类 。 Use Unmarshaller
to populate auto-generated classes from XML file: 使用Unmarshaller
从XML文件填充自动生成的类:
Unmarshaller u = jc.createUnmarshaller(); Orders os = (Orders) u.unmarshal( new FileInputStream( "orders.xml" ) );
That's it... JaxB will take care of classes, attributes, populate, write/read xml... 就这样... JaxB将处理类,属性,填充,写入/读取xml ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.