0

I want to parse title and link tag only in item section with XMLpullparser. how do i parse it with ignoring previous title and link tag?

<channel>
 <title>AAA</title>
 <link>linkone</link>
 <item>
  <title>BBB</title>
  <link>link2</link>
 </item>
</channel>

The parser which i am using currently is following. How do ignore starting title and link tag?

try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser xpp = factory.newPullParser();

        FileInputStream fis = ctx.openFileInput("StackSites.xml");
        BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

        xpp.setInput(reader);

        int eventtype = xpp.getEventType();
        while (eventtype != XmlPullParser.END_DOCUMENT){
            String tagname = xpp.getName();

            switch (eventtype){
                case XmlPullParser.START_TAG:
                    if(tagname.equalsIgnoreCase("item")){
                        curStackSite = new StackSite();
                    }
                    break;

                case  XmlPullParser.TEXT:
                    curText = xpp.getText();
                    break;

                case XmlPullParser.END_TAG:
                    if(tagname.equalsIgnoreCase("item")){
                        stackSites.add(curStackSite);
                    }
                    if(tagname.equalsIgnoreCase("title")){
                        curStackSite.setName(curText);
                    }
                    if(tagname.equalsIgnoreCase("link")){
                        curStackSite.setLink(curText);
                    }
                    break;
                default:
                    break;
            }
            eventtype = xpp.next();
        }
0

2 Answers 2

1

I dont know wheather its right way to do it, but what i did is inside case XmlPullParser.START_TAG: if it found item tag , ive set counter is equal to 1. and in case XmlPullParser.END_TAG: if counter != 0, then only it adds value to object .this method works. if anybody knows more accurate way please post. Thanks.

Sign up to request clarification or add additional context in comments.

Comments

0

There is one library available to parse xml response to POJO class. https://github.com/stanfy/gson-xml

By using this library, you do not need to do manual parsing. You need to create POJO class according to your xml.

For given xml, you need to create two POJO classes

Channel.java

public class Channel {
    Item item;
}

Item.java

public class Item {
    String title;
    String link;
}

Now you can parse your xml like below

public void parseXml(Strng xml){
    XmlParserCreator parserCreator = new XmlParserCreator() {
            @Override
            public XmlPullParser createParser() {
                try {
                    return   XmlPullParserFactory.newInstance().newPullParser();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
        }
    };

    GsonXml gsonXml = new GsonXmlBuilder().setXmlParserCreator(parserCreator).create();

    Channel channel = gsonXml.fromXml(xml, Channel.class);
    Log.v("temp", "Title : " + channel.item.title);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.