How can i identify same xml tag second time
Asked Answered
B

1

0

I am not full aware of XMLParsing so i am beginner in XMLParsing. I am using XMLPull parser.

i wanna to store player one and player two in below xml. How can i identify playertag??

<scores sport="tennis">
<category name="Atp - Doubles: Us Open (Usa), Hard" city="Us Open" id="11987">
<match date="08.09.2016" time="19:00" status="Not Started" id="396388">
<player name="Herbert/ Mahut" s1="" s2="" s3="" s4="" s5="" totalscore="" id="22079"/>
<player name="Murray/ Soares" s1="" s2="" s3="" s4="" s5="" totalscore="" id="33098"/>
</match>
<match date="08.09.2016" time="20:30" status="Not Started" id="396400">
<player name="Lopez/ Lopez" s1="" s2="" s3="" s4="" s5="" totalscore="" id="9632"/>
<player name="Carreno-Busta/ Garcia-Lopez" s1="" s2="" s3="" s4="" s5="" totalscore="" id="22770"/>
</match>
</category>
</scores>

Here is my code

public List<LiveScore> parse(InputStream is) {
        try {

            String tournament = null,tournament_place = null,date = null,time = null,status = null,player_one = null,player_two;
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser parser = factory.newPullParser();

            parser.setInput(is, null);

            liveScoreArrayList = new ArrayList<LiveScore>();

            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch (eventType) {
                    case XmlPullParser.START_TAG:

                        if ("category".equals(tagname)){

                            tournament = parser.getAttributeValue(null,"name");
                            tournament_place = parser.getAttributeValue(null,"city");

                        }

                        if ("match".equals(tagname)){

                            date = parser.getAttributeValue(null,"date");
                            time = parser.getAttributeValue(null,"time");
                            status = parser.getAttributeValue(null,"status");

                        }

                        break;

                    case XmlPullParser.TEXT:

//                        text = parser.getText();
//                        Log.i(TAG, "parse: text: "+text);

                        break;

                    case XmlPullParser.END_TAG:

                        if ("player".equals(tagname)){

                            player_one = parser.getAttributeValue(null,"name");

                        }



                        Log.d("LiveMatch", "parse: category : "+tournament+" city = "+tournament_place+" date = "+date+ " time = "+time+" status = "+status+" player_one = "+player_one);

//                        rankList.add(new Rank(rank,player_name+" \n"+country,points ));


                        break;

                    default:
                        break;
                }
                eventType = parser.next();
            }

        } catch (XmlPullParserException e) {e.printStackTrace();}
        catch (IOException e) {e.printStackTrace();}

        return liveScoreArrayList;
    }
Bowleg answered 8/9, 2016 at 12:42 Comment(0)
O
0

This is Complete Xml Pull Parsing example.. It may Help you.

https://androidgarden.wordpress.com/2016/09/02/android-xml-pull-parsing-from-sd-card-assets/

Ophiology answered 4/10, 2016 at 4:0 Comment(2)
but in this example xml is totally different.Bowleg
Thanks for the answerBowleg

© 2022 - 2024 — McMap. All rights reserved.