Reputation: 1521
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;
}
Upvotes: 0
Views: 241
Reputation: 966
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
Upvotes: 2