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!

2 Answers 2

1

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();
        }
Sign up to request clarification or add additional context in comments.

Comments

0
  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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.