staxx从元素中获取属性值

时间:2011-12-14 18:46:19

标签: java xml xpath sax stax

我尝试使用STAX从URI解析XML文件!

XML看起来像这样:

   <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>
输出应该是:

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

我的代码看起来像这样:

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);

                            }


                        }
                    }
                }

            }

此代码生成输出:

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

如何在元素“Occurrence”中从属性“decimal”中获取其他值?

1 个答案:

答案 0 :(得分:2)

  

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

我只是快速浏览一下,但我会说你可能会留在第一个Occurence元素,因为你只调用一次streamReader.nextTag(),但是调用streamReader.getAttributeValue三次。