Question

So I've got to parse ugly files that contain nested tags like

<p>blah<strong>lah</strong>blah</p>

The nested tags are defined and I don't care about them. But they make XmlPullParser fail:

XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setInput(some_reader);
while (parser.next() != XmlPullParser.END_DOCUMENT) {
    if (XmlPullParser.START_TAG == event) {
        String tag = parser.getName();
        if (tag != null) {
            tag = tag.toLowerCase();
        } else {
            continue;
        }
       if ("p".equals(tag)) {
           String text = parser.nextText();
           // and here we go
           // org.xmlpull.v1.XmlPullParserException: expected: /p read: strong
        }
    }
}

Question: any chance I could get away w/o preprocessing the file stripping all the unnecessary tags or using a third-party library?

EDIT: Updated the snippet to actually make sense.

Was it helpful?

Solution

So I've got rid of XMLPullParser and switched to SAXParser. Besides, it performs better.

OTHER TIPS

package com.xml;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;

public class FeedHandler extends DefaultHandler {

    StringBuilder sb = null;
    String ret = "";
    boolean bStore = false;
    int howMany = 0;

    FeedHandler() {   }

    String getResults()
    {
        return "XML parsed data.\nThere are [" + howMany + "] status updates\n\n" + ret;
    }
    @Override
    public void startDocument() throws SAXException 
    {
        // initialize "list"
    }

    @Override
    public void endDocument() throws SAXException
    {

    }

    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {

        try {
            if (localName.equals("status"))
            {
                this.sb = new StringBuilder("");
                bStore = true;
            }
            if (localName.equals("user")) 
            {
                bStore = false;
            }
            if (localName.equals("text")) 
            {
                this.sb = new StringBuilder("");
            }
            if (localName.equals("created_at")) 
            {
                this.sb = new StringBuilder("");
            }
        } catch (Exception e) 
        {

            Log.d("error in startElement", e.getStackTrace().toString());
        }
    }
    @Override

    public void endElement(String namespaceURI, String localName, String qName) throws SAXException 
    {

        if (bStore) 
        {
            if (localName.equals("created_at"))
            {
                ret += "Date: " + sb.toString() + "\n"; 
                sb = new StringBuilder("");
                return;

            }

            if (localName.equals("user"))
            {
                bStore = true;
            }

            if (localName.equals("text")) 
            {

                ret += "Post: " + sb.toString() + "\n\n";
                sb = new StringBuilder("");
                return;

            }


        }
        if (localName.equals("status"))
        {
            howMany++;
            bStore = false;
        }
    }
    @Override

    public void characters(char ch[], int start, int length)
    {

        if (bStore) 
        {
            String theString = new String(ch, start, length);

            this.sb.append(theString);
        }
    }

}

and this my xmlActivity class that extend Activity

            InputSource is = new InputSource(getResources().openRawResource(R.raw.my));
            System.out.println("running xml file..... ");
        // create the factory
        SAXParserFactory factory = SAXParserFactory.newInstance();

        // create a parser
        SAXParser parser = factory.newSAXParser();

        // create the reader (scanner)
        XMLReader xmlreader = parser.getXMLReader();

        // instantiate our handler
        FeedHandler fh = new FeedHandler();

        // assign our handler
        xmlreader.setContentHandler(fh);

        // perform the synchronous parse
        xmlreader.parse(is);

        // should be done... let's display our results
        tvData.setText(fh.getResults());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top