1
我嘗試瞭解如何工作STAX java的機制。如何讀/解析XML STAX(機制)
我有這個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>
要模仿我們創建具有類似屬性
public class Product {
private int orderID;
private String createTime;
private String description;
private String gtin;
private String price;
private String supplier;
//getter and setter
}
有了這個,我想讀我的XML文件中的對象這個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());
}
我的問題是,他們有5個商品,但是當我嘗試將它們輸出,是不正確的號碼。 1.如果:如果(。startElement.getName()getLocalPart()等於(「訂單」))的最後一個參數是在輸出我看到的只是一個對象(松下TC-L47E50 47" 智能電視維埃拉「oders」 E50系列LED HDTV)
- 如果最後一個參數是在我的輸出順序 2個對象
- 如果最後一個參數是「產品」在我的輸出我有所有的人, 5.
我需要做什麼修改,以讀取整個信息我的範圍是閱讀訂單的屬性「創建」和「ID」,但與我所有的對象,而不是1或2 謝謝!
這是很容易爲你,因爲你有經驗........對我來說它是一個新的開始新的文件: - < – Silviu
只要按照提供的步驟和環節,它很容易,你希望 –
如果評論你會陷入任何一步! –