Question

it's the first time that I'm trying to parse an xml file so I need a little help of understanding how to get all the information that I need from an xml string in my android application. Here is my xml String :

<ASX VERSION="3.0"><TITLE>Hits80and90.Com</TITLE><ENTRY>        <TITLE>Hits 80s and 90s</TITLE>       <REF HREF= "http://server-uk3.radioseninternetuy.com:11168"/>      <AUTHOR>Hits80and90.Com</AUTHOR>      <Abstract>The Best Music On The Web</ABSTRACT>       </ENTRY></ASX>

How can I get all the information in <ENTRY> tags like title, link and etc?

Thanks for any kind of help!

Was it helpful?

Solution 2

This is what you can use to parse your XML String :

import java.io.IOException;
import java.io.StringReader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class AsxParser {

    XmlPullParser xmlpullparser;

    public void AscParser(){

    }

    public void parsing(String url){
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            xmlpullparser = factory.newPullParser();
            xmlpullparser.setInput(new StringReader(url));

            int eventType = 0;  
            try {  
                eventType = xmlpullparser.getEventType();  
            } catch (XmlPullParserException e) {  
                e.printStackTrace();  
            }  
            while (eventType != XmlPullParser.END_DOCUMENT) {  

                parseTag(eventType);  
                try {  
                    eventType = xmlpullparser.next();  
                } catch (XmlPullParserException e) {  
                    e.printStackTrace();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }


        }catch(Exception e){
            e.printStackTrace();
        }
    }

    void parseTag(int event){  

        switch (event) {  

        case XmlPullParser.START_DOCUMENT:  
            Log.i("","START_DOCUMENT");  
            break;  

        case XmlPullParser.END_DOCUMENT:  
            Log.i("","END_DOCUMENT");  
            break;  
        case XmlPullParser.START_TAG:  
            Log.i("","START_TAG : "+xmlpullparser.getName());  
            Log.i("","Attribute Name : "+xmlpullparser.getAttributeValue(null,"HREF"));  

            break;  

        case XmlPullParser.END_TAG:
            Log.i("","END_TAG : "+xmlpullparser.getName());
            break;

        case XmlPullParser.TEXT:
            Log.i("","TEXT");
            String output = xmlpullparser.getText();  
            Log.i("valuee : ",""+output);  
            break;  
        }  

    }  

}

OTHER TIPS

you can use a sax parser, i wrote a little piece of code to show you how to get the title in the entry. I didn't test it but I think it will help you to parse an XML file with sax.

Here the code :

import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaxParserContent {

public static void parse(String data) {

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SaxParserContent.HandlerContent handler = new HandlerContent();
    try {
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(new InputSource(new StringReader(data)), handler);

    } catch (Throwable err) {
    }
}

public static class HandlerContent extends DefaultHandler {

    protected boolean inEntry = true;
    protected boolean inTitle = true;
    protected String title;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (localName.equals("ENTRY")) {
            inEntry = true;
        }
        if (inEntry == true && localName.equals("TITLE")) {
            inTitle = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (localName.equals("ENTRY")) {
            inEntry = false;
        }
        if (inEntry == true && localName.equals("TITLE")) {
            inTitle = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (inEntry == true && inTitle == true) {
            title = new String(ch);
        }
    }

    public String getTitle() {
        return title;
    }
}

}

Good luck !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top