Android Xml Parsing "embedded values"

All we need is a simple explanation of the problem, which is provided below.

Im having a hard time trying to figure out how to parse this block of data:

<prov version="1.1">
  <characteristic type="section1">
    <parm name="version" value="74"/>
    <parm name="validity" value="172800"/>
  </characteristic>
...
  <characteristic type="section99">
    <parm name="random_setting1" value="blahblah1"/>
    <parm name="random_setting2" value="blahblah2"/>
    <characteristic type="section99_subsection2">
      <parm name="random_setting3" value="blahblah1"/>
      <parm name="random_setting4" value="blahblah2"/>
    </characteristic> 
  </characteristic>
</prov>

I have probably 200+ lines in an xml file resembling the above example. Id like to break it up into 4 fields to put in a database:

1stlevel characteristic

2ndlevel characteristic (can be null)

settingName

value

But I cannot for the life of me figure out how to do this.

I have this document builder:

        Log.d("RNM", "Starting xmlToDb");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document xmlParse = dBuilder.parse(new InputSource(new StringReader(xmlString)));
        xmlParse.getDocumentElement().normalize();
        NodeList nList = xmlParse.getElementsByTagName("characteristic");
        for ( int tmp = 0; tmp < nList.getLength(); tmp++) {
            Node nNode = nList.item(tmp);
            System.out.println("\nCurrent Element :" + nNode.getNodeName());
            if ( nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                String charType = eElement.getAttribute("type"); //Tells me the value of characteristic
            }
        }

The above works to pull out all the characteristic blocks and can get the values. but I can’t figure out how to extract the parmName and parmValud that lie beneath each one.

Any examples out there on dealing with this? I looked here: http://theopentutorials.com/tutorials/android/xml/android-simple-xml-sax-parser-tutorial/ But I could not figure out how to grab those values with the saxparser.

Let’s Solve it:

This is a common error many developers questioned us about it. So we write the explanation above. You just have to apply the suggested solution to your code and it will do for you. If you still getting this error after applying this code then comment below we will get back to you with the new method.

Solution 1

I use Android’s XmlPullParser.

There are two key methods: next() and nextToken(). While next() provides access to high level parsing events, nextToken() allows access to lower level tokens.

An example usage:

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

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

public class SimpleXmlPullApp
{
     public static void main (String args[])
         throws XmlPullParserException, IOException
     {
         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         factory.setNamespaceAware(true);
         XmlPullParser xpp = factory.newPullParser();

         xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
         int eventType = xpp.getEventType();
         while (eventType != XmlPullParser.END_DOCUMENT) {
          if(eventType == XmlPullParser.START_DOCUMENT) {
              System.out.println("Start document");
          } else if(eventType == XmlPullParser.START_TAG) {
              System.out.println("Start tag "+xpp.getName());
          } else if(eventType == XmlPullParser.END_TAG) {
              System.out.println("End tag "+xpp.getName());
          } else if(eventType == XmlPullParser.TEXT) {
              System.out.println("Text "+xpp.getText());
          }
          eventType = xpp.next();
         }
         System.out.println("End document");
     }
}

Note: You are free to use these solutions for your personal use. We recommend you apply the first solution to your code because it was tested in our system before posting it on this page.

We are always trying to help the developer community, So we made their work easy. Basically, we collected these data from stackoverflow.com, As it is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0.

Leave a Comment