1

I am parsing XML with DOM i have some data tags like:

<data>
    <option>abc</option>
    <option>ijk</option>
    <option>fgh</option>
    <option>njk</option>
    <option>klj</option>
    <option>opi</option>
</data>
<data>
    <option>abc</option>
    <option>ijk</option>
    <option>fgh</option>
    <option>njk</option>
    <option>klj</option>
    <option>opi</option>
</data>

I want to insert the options in the list in my layout how can I do the same pls tell. I want that it just parse data of first block and for the next it asks for the trigger event like by clickin on a button.

Thanx in advance.

Aashutosh Sharma
  • 1,483
  • 2
  • 17
  • 29

2 Answers2

1

Since you want to parse partial XML data, it would be better to use the XmlPullParser.

You would need to have a method that reads one block at a time keeping a reference to the parser as a member variable.

public class PartialXmlParser{
    private XmlPullParser xpp;

    public PartialXmlParser(String xml){
         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         factory.setNamespaceAware(true);
         xpp = factory.newPullParser();

         xpp.setInput( new StringReader (xml ) );
    }

    public List<String> getData(){
        List<String> retValue = new List<String>();
        //Logic to read one block of data and add to retValue
        //
        return retValue;

    }
}

and call the getData method in the callback event (like OnClick of the button). Remember to use the same instance of the object for getting the data.

Rajesh
  • 15,724
  • 7
  • 46
  • 95
0

The Java platform has supported many different ways to work with XML for quite some time, and most of Java's XML-related APIs are fully supported on Android. For example, Java's Simple API for XML (SAX) and the Document Object Model (DOM) are both available on Android. Both of these APIs have been part of Java technology for many years. The newer Streaming API for XML (StAX) is not available in Android(More).

This is the best example for how to parsing xml in android just like your problem.. Here is the Android XML SAX Parser Example .Parsing XML in Android (DOM method) is antoher best example for Dom Methodhttp://www.androidhive.info/2011/11/android-xml-parsing-tutorial/.

XmlResourceParser is the XML parsing interface returned for an XML resource. This is a standard XmlPullParser interface, as well as an extended Attribute Set interface and an additional close() method on this interface for the client to indicate when it is done reading the resource(More).

and dont forget to see this. Parsers used in Android

Community
  • 1
  • 1