how is read/parsing xml java STAX (mechanism)

Silviu

I try to understand how is working the mechanism of STAX java.

I have this xml file

<?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

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:

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. 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 last parameter is order in my output are 2 objects
  2. if last parameter is "product" in my output I have all of them, 5.

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 !

Jordi Castilla

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! :)


Basic steps to read-write xml using JaxB / Unmarshaller and XSD

  • Create a valid XSD file of your XML structure.
  • Place it in your project folder.
  • Right click XSD file and auto-generate JAXB classes.
  • Use Unmarshaller to populate auto-generated classes from XML file:

    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...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how is read/parsing xml java STAX (mechanism)

From Dev

Java Stax for Complex / Large XML

From Dev

How to get XML element path using stax/stax2?

From Dev

How do I remove elements and their children from Xml using Java and Stax

From Dev

java.net.MalformedURLException - at parsing XML file by StAX

From Dev

JAVA get attributes value from xml using stax parser

From Dev

java.net.MalformedURLException - at parsing XML file by StAX

From Dev

Parse XML using Java StAX - Count number of content tags

From Dev

How to extract attribute values from xml file with Stax?

From Dev

How to extract attribute values from xml file with Stax?

From Dev

Writing xml string with StAX

From Dev

Writing stax XML to String

From Dev

how to get the child node value on attribute stax Java

From Dev

stax xml confusion with getname function

From Dev

Custom StAX Parser for XML using javax wrappers

From Dev

Error parsing XML with DTD using Stax

From Dev

Modifying one XML file using StAX

From Dev

Parse special characters in xml stax file

From Dev

StAX well-formedness check of XML

From Dev

Error parsing XML with DTD using Stax

From Dev

How do I fix this transforming the stax source error in Java when using Spring-WS?

From Dev

java StAX parser: not preserving double quotes for attributes

From Dev

xml validation with StAX: Unrecognized property 'javax.xml.stream.isInterning'

From Dev

How to extend java classes on in Python with JPype as its interfacing mechanism with java?

From Dev

Problems getting XML node text in StAX XMLStreamConstants.CHARACTERS event

From Dev

Can a part of XML be marshalled using JAXB (or JAXB + StAX)?

From Dev

StAX with JAXB XML parsing Only Parsing first instance

From Dev

Unmarshalling XML to three lists of different objects using STAX Parser

From Dev

Problems getting XML node text in StAX XMLStreamConstants.CHARACTERS event

Related Related

  1. 1

    how is read/parsing xml java STAX (mechanism)

  2. 2

    Java Stax for Complex / Large XML

  3. 3

    How to get XML element path using stax/stax2?

  4. 4

    How do I remove elements and their children from Xml using Java and Stax

  5. 5

    java.net.MalformedURLException - at parsing XML file by StAX

  6. 6

    JAVA get attributes value from xml using stax parser

  7. 7

    java.net.MalformedURLException - at parsing XML file by StAX

  8. 8

    Parse XML using Java StAX - Count number of content tags

  9. 9

    How to extract attribute values from xml file with Stax?

  10. 10

    How to extract attribute values from xml file with Stax?

  11. 11

    Writing xml string with StAX

  12. 12

    Writing stax XML to String

  13. 13

    how to get the child node value on attribute stax Java

  14. 14

    stax xml confusion with getname function

  15. 15

    Custom StAX Parser for XML using javax wrappers

  16. 16

    Error parsing XML with DTD using Stax

  17. 17

    Modifying one XML file using StAX

  18. 18

    Parse special characters in xml stax file

  19. 19

    StAX well-formedness check of XML

  20. 20

    Error parsing XML with DTD using Stax

  21. 21

    How do I fix this transforming the stax source error in Java when using Spring-WS?

  22. 22

    java StAX parser: not preserving double quotes for attributes

  23. 23

    xml validation with StAX: Unrecognized property 'javax.xml.stream.isInterning'

  24. 24

    How to extend java classes on in Python with JPype as its interfacing mechanism with java?

  25. 25

    Problems getting XML node text in StAX XMLStreamConstants.CHARACTERS event

  26. 26

    Can a part of XML be marshalled using JAXB (or JAXB + StAX)?

  27. 27

    StAX with JAXB XML parsing Only Parsing first instance

  28. 28

    Unmarshalling XML to three lists of different objects using STAX Parser

  29. 29

    Problems getting XML node text in StAX XMLStreamConstants.CHARACTERS event

HotTag

Archive