tags:

views:

139

answers:

2

The API I need to work with does not support xpath, which is a bit of a headache! :-( lol

The xml I want to parse is as a String. My questions:

  1. Is there a Java equivalent of "simplexml_load_string", where it makes the string into an xml document for parsing?
  2. Which is better for parsing, SAX or DOM? I need to get a couple of values out of the XML and the structure isn't that deep. [3 levels]

Thanks!

+1  A: 

Maybe this will help you

//http://developer.android.com/intl/de/reference/android/content/res/XmlResourceParser.html
import org.xmlpull.v1.XmlPullParserException;
try {
            XmlResourceParser xrp = ctx.getResources().getXml(R.xml.rules);
            while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
                if (xrp.getEventType() == XmlResourceParser.START_TAG) {
                    String s = xrp.getName();
                    if (s.equals("category")) {

                        String catname = xrp.getAttributeValue(null, "name");

                        String rule = xrp.getAttributeValue(null, "rule");



                    }
                } else if (xrp.getEventType() == XmlResourceParser.END_TAG) {
                    ;
                } else if (xrp.getEventType() == XmlResourceParser.TEXT) {
                    ;
                }
                xrp.next();
            }
            xrp.close();

        } catch (XmlPullParserException xppe) {
            Log.e(TAG(), "Failure of .getEventType or .next, probably bad file format");
            xppe.toString();
        } catch (IOException ioe) {
            Log.e(TAG(), "Unable to read resource file");
            ioe.printStackTrace();
        }
Pentium10
A: 
  1. Not sure.
  2. If the XML file/string is small, DOM is a good choice as it provides more capability. SAX should be used for larger XML files where memory usage and performance is a concern.
DiskCrasher