簡體   English   中英

如何讀取/解析xml Java STAX(機制)

[英]how is read/parsing xml java STAX (mechanism)

我嘗試了解STAX java的工作原理。

我有這個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>

為了模仿此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
}

我試圖以此讀取我的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.如果在:if(startElement.getName()。getLocalPart()。equals(“ orders”))中,最后一個參數是輸出中的“ oders”,那么我只會看到一個對象(Panasonic TC-L47E50 47“ Smart TV Viera E50系列LED HDTV)

  1. 如果最后一個參數是我的輸出中的訂單 ,則為2個對象
  2. 如果我的輸出中最后一個參數是“ product”,則所有參數都為5。

我需要做些修改,以讀取全部信息。 我的范圍是讀取屬性“ created”和“ id”的順序,但要讀取所有對象,而不是1或2。謝謝!

如果您使用的是JaxB ,那么您甚至都不需要自己創建類,甚至不用自己解析XML文件, 這就是JaxB是為之制造的! :)


使用JaxB / UnmarshallerXSD讀寫xml的基本步驟

  • 創建您的XML結構的有效 XSD文件。
  • 將其放在您的項目文件夾中。
  • 右鍵單擊XSD文件並自動生成JAXB類
  • 使用Unmarshaller從XML文件填充自動生成的類:

     Unmarshaller u = jc.createUnmarshaller(); Orders os = (Orders) u.unmarshal( new FileInputStream( "orders.xml" ) ); 

就這樣... JaxB將處理類,屬性,填充,寫入/讀取xml ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM