【问题标题】:how is read/parsing xml java STAX (mechanism)如何读取/解析xml java STAX(机制)
【发布时间】:2015-09-18 11:56:52
【问题描述】:

我试图了解 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 高清电视)

  1. 如果最后一个参数是 order 在我的输出中是 2 个对象
  2. 如果我的输出中最后一个参数是“product”,那么我就有了所有这些参数,5。

为了阅读全部信息,我需要做哪些修改。我的范围是读取 order 的属性“created”和“id”,但使用我的所有对象,而不是 1 或 2 谢谢!

【问题讨论】:

    标签: java xml stax


    【解决方案1】:

    如果您使用JaxB,您甚至不需要自己创建类甚至解析 XML 文件,这就是 JaxB 的用途! :)


    使用JaxB/UnmarshallerXSD读写xml的基本步骤

    • 为您的XML 结构创建一个有效 XSD 文件。
    • 将它放在您的项目文件夹中。
    • 右击XSD文件和auto-generate JAXB classes
    • 使用Unmarshaller 从 XML 文件填充自动生成的类:

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

    就是这样……JaxB 将负责类、属性、填充、写入/读取 xml……

    【讨论】:

    • 这对你来说很容易,因为你有经验........对我来说它是新文档的新开始:-
    • 只需按照提供的步骤和链接进行操作,比您想象的要容易
    • 如果您卡在任何步骤中,请发表评论!
    猜你喜欢
    • 2015-12-29
    • 2016-10-30
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    相关资源
    最近更新 更多