0

example This is and XML from site

<game>
    <root>
        <menu>
            <category>
                <item>aa</item>
                <item>bb</item>
            </category>
        </menu>
    </root>
</game>

using parser.require(XmlPullParser.START_TAG, null, "category");

how to get the value of the two items? or is there more good ways to find go directly your target nodes or items? this is my codes

String search = null;

public static List<String> parse(InputStream stream, String search)
                throws XmlPullParserException, IOException {
            Util2.XmlParser.search = search;
            try {
                XmlPullParser parser = Xml.newPullParser();
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,
                        false);
                parser.setInput(stream, null);
                parser.nextTag();
                return readItem(parser);


            } finally {
                stream.close();
            }
        }

        public static List<String> readItem(XmlPullParser parser) throws XmlPullParserException, IOException {
            List<String> entries = new ArrayList<String>();

            parser.require(XmlPullParser.START_TAG, null, "category");
            while (parser.next() != XmlPullParser.END_TAG) {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
                String name = parser.getName();
                // Starts by looking for the entry tag
                if (name.equals("item")) {
                    System.out.println(parser.getText());
                    System.out.println(parser.getName());

                } else {
                    //skip(parser);
                }
            }  
            return entries;
        }

1 Answer 1

2

If you look into the sources of "require" method, you'll see that it just checker:

public void require(int type, String namespace, String name)
        throws XmlPullParserException, IOException {
    if (type != this.type
            || (namespace != null && !namespace.equals(getNamespace()))
            || (name != null && !name.equals(getName()))) {
        throw new XmlPullParserException(
                "expected: " + TYPES[type] + " {" + namespace + "}" + name, this, null);
    }
}

You need to walk all path until you reach what you want:

<a> -> <aa> -> <aaa>

I would suggest you to have reader method for each tag and call them when you are sure you reached the tag.

And this is nice example: http://developer.android.com/training/basics/network-ops/xml.html

Sign up to request clarification or add additional context in comments.

3 Comments

it does not go directly to your desired target nodes, still you just have to walk all path until you reach it?
so the first .require should be top most nodes? start from the top nodes ?
It is not necessity, it's just for insurance. You can put it everywhere.

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.