Android Xml Parsing "embedded values"

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.

Answers


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");
     }
}

Need Your Help

Magento Error when Disable module

magento module attributes

I created a module then use upgrade script to add a multiselect attribute. the attribute is using 'source' to get it values dynamically. the code is the following: