2

I try to parse XML file from URI using STAX!

XML looking something like this:

   <Type type_id="4218">
        <Title>English Premier League</Title>

        <Event start_time="2011-12-18 16:10:00" ev_id="2893772">
              <Description>Manchester City v Arsenal</Description>

                   <Market mkt_typ="Win/Draw/Win">
                         <Occurrence bet_id="42455761"  decimal="1.6666666666667">
                               <Description>Manchester City</Description>
                         </Occurrence>
                         <Occurrence bet_id="42455762"  decimal="3.6">
                               <Description>Draw</Description>
                         </Occurrence>
                         <Occurrence bet_id="42455764"  decimal="5">
                          <Description>Arsenal</Description>
                          </Occurrence>
                    </Market>

          </Event>
    </Type>

output should be:

id:4218
title:English Premier League
ev_id:2893772
date of match:Sun Dec 18 16:10:00 CET 2011
description:Manchester City v Arsenal
one:1.6666666666667
draw:3.6
two:5

My code looks something like this:

XMLInputFactory factory = XMLInputFactory.newInstance();

        try {
            XMLStreamReader streamReader = factory.createXMLStreamReader(new URL("http://cubs.bluesq.com/cubs/cubs.php?action=getpage&thepage=385.xml").openStream());


            while (streamReader.hasNext()) {
                int event = streamReader.next();

                if(event == XMLStreamConstants.START_ELEMENT){
                    if(streamReader.getLocalName().equals("Type")){
                        long id = Integer.parseInt(streamReader.getAttributeValue(null, "type_id"));
                        if( id == 4218){
                            System.out.println("id:"+id);
                            streamReader.nextTag();
                            System.out.println("title:"+streamReader.getElementText());
                            streamReader.nextTag();
                            int ev_id = Integer.parseInt(streamReader.getAttributeValue(null, "ev_id"));
                            System.out.println("ev_id:"+ev_id);
                            DateFormat formater = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                            formater.setTimeZone(TimeZone.getTimeZone("CET"));
                            String tempDatum = streamReader.getAttributeValue(null,"start_time");
                            Date dateOfMatch = formater.parse(tempDatum);

                            System.out.println("date of match:"+dateOfMatch);
                            streamReader.nextTag();
                            String description = streamReader.getElementText();

                            System.out.println("description:"+ description);
                            streamReader.nextTag();
                            String market = streamReader.getAttributeValue(null, "mkt_typ");
                            if(market.equals("Win/Draw/Win")){
                                streamReader.nextTag();
                                    double one = Double.parseDouble(streamReader.getAttributeValue(null, "decimal"));
                                    System.out.println("one:"+ one);

                                    double draw = Double.parseDouble(streamReader.getAttributeValue(null, "decimal"));
                                    System.out.println("draw:"+draw);
                                    double two = Double.parseDouble(streamReader.getAttributeValue(null, "decimal"));
                                    System.out.println("two:"+two);

                            }


                        }
                    }
                }

            }

This code produce output:

id:4218
title:English Premier League
ev_id:2893772
date of match:Sun Dec 18 16:10:00 CET 2011
description:Manchester City v Arsenal
one:1.6666666666667
draw:1.6666666666667
two:1.6666666666667

how do I get others value from atribute "decimal" in elements "Occurrence"???

dusmanka
  • 625
  • 1
  • 9
  • 16
  • This looks like a job for XSLT and/or XPath...I'm sure you have your reasons for using this library, and I have no idea if it's any good or not, but I do know that XPath and XSLT are awesome, and they (particularly XPath) could output your results pretty easily (IMHO) – Cody S Dec 14 '11 at 18:55
  • I think this is exactly job for SAX or StAX, because he is iterating through all elements and all data in this XML. – viktor Dec 14 '11 at 20:18
  • @Cody S Xpath can't use on Stax http://stackoverflow.com/questions/1863250/is-it-there-any-xpath-processor-for-sax-model – dusmanka Dec 15 '11 at 09:20

2 Answers2

2

if(market.equals("Win/Draw/Win")){ streamReader.nextTag();

I only had a quick look, but I would say you probably are staying at the first Occurence element since you're calling streamReader.nextTag() only once, but streamReader.getAttributeValue three times.

Puce
  • 37,247
  • 13
  • 80
  • 152
0

private String xmlFile = "file location"; private String desiredAttribute;

...

ClassLoader classLoader = this.getClass().getClassLoader();

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    InputStream inputStream = classLoader.getResourceAsStream(xmlFile);
    XMLEventReader xmlEventReader = inputFactory.createXMLEventReader(inputStream);

try {

        while (xmlEventReader.hasNext()) {
            XMLEvent event = xmlEventReader.nextEvent();

            if (event.isStartElement()) {
                StartElement startElement = event.asStartElement();

                QName qname = startElement.getName();
                String elementName = qname.toString();
                if (elementName.equals("My Element")) {
                    Iterator attributes = startElement.getAttributes();
                    while (attributes.hasNext()) {
                        Attribute attribute = (javax.xml.stream.events.Attribute) (attributes.next());
                        String attribute= attribute.getValue();

                        desiredAttribute = attribute;
                    }

...

You can also check for attribute name while iterating

dfostic
  • 1,608
  • 15
  • 8