Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to parse XML (org.w3c.Document) on Android

Tags:

android

xml

Can anyone point me to a explanation for or explain to me how I can easily parse the XML and get values of a w3c.Document on Android using only Android OS Libs?

I tried to use a implementation of dom4j, but it is very slow :-(

like image 564
Thizzer Avatar asked Dec 18 '09 10:12

Thizzer


People also ask

Which XML parser is recommended for Android?

Among all of them android recommend XMLPullParser because it is efficient and easy to use. So we are going to use XMLPullParser for parsing XML.

How do you parse an XML file?

To parse XML documents, use the XML PARSE statement, specifying the XML document that is to be parsed and the processing procedure for handling XML events that occur during parsing, as shown in the following code fragment.

What is XML parsing in Android?

Android DOM(Document Object Model) parser is a program that parses an XML document and extracts the required information from it. This parser uses an object-based approach for creating and parsing the XML files. In General, a DOM parser loads the XML file into the Android memory to parse the XML document.

What are the two methods of parsing in XML document?

To read and update, create and manipulate an XML document, you will need an XML parser. In PHP there are two major types of XML parsers: Tree-Based Parsers. Event-Based Parsers.


1 Answers

Here's an article at Developer.com comparing the performance of the DOM, SAX and Pull parsers on Android. It found the DOM parser to be by far the slowest, then the Pull parser and the SAX parser the fastest in their test.

If you're going to be a doing a lot of parsing in your application it may be worth benchmarking the different options to see which works best for you.

I've used the XmlPullParser via XmlResourceParser and found that worked well and was easy to use.

It works through the XML returning events telling you what it's found in there.

If you use it, your code will look something like this:

XmlResourceParser parser = context.getResources().getXml(R.xml.myfile);

try {
    int eventType = parser.getEventType();

    while (eventType != XmlPullParser.END_DOCUMENT) {
        String name = null;

        switch (eventType){
            case XmlPullParser.START_TAG:
                name = parser.getName().toLowerCase();

                if (name.equals(SOME_TAG)) {
                    for (int i = 0;i < parser.getAttributeCount();i++) {
                        String attribute = parser.getAttributeName(i).toLowerCase();

                        if (attribute.equals("myattribute")) {
                            String value = parser.getAttributeValue(i);
                        }

                    }
                }

                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                break;
        }

        eventType = parser.next();
    }
}
catch (XmlPullParserException e) {
    throw new RuntimeException("Cannot parse XML");
}
catch (IOException e) {
    throw new RuntimeException("Cannot parse XML");
}
finally {
    parser.close();
}

If you want to parse XML that's not from a resource file you could create a new XmlPullParser using the XmlPullParserFactory class and then call the setInput() method.

like image 142
Dave Webb Avatar answered Oct 20 '22 01:10

Dave Webb