I am trying to parse an XML document and return a single object, named System. Instead, for every end tag in the XML document, and new System is returned. I tried to move where I create the System to various parts of the code, but either the System object would only be nulls, or there would be an error about no return statement. Here is the original code before I tried to move where I created the System:
private System readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
//TODO Make it return one system per time through, not multiple after every end tag
parser.require(XmlPullParser.START_TAG, ns, null);
String systemName = null;
String rightAscension = null;
String declination = null;
String distance = null;
while (parser.getEventType() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
Log.i("String", "Start Tag Added");
continue;
}
String name = parser.getName();
if (name.equalsIgnoreCase("name")) {
Log.i("Name:", "Name added to parser");
systemName = readName(parser);
} else if (name.equalsIgnoreCase("rightascension")) {
Log.i("RAscen:", "Right Ascension added to parser");
rightAscension = readRightAscension(parser);
} else if (name.equalsIgnoreCase("declination")) {
Log.i("Dec:", "Declination added to parser");
declination = readDeclination(parser);
} else if (name.equalsIgnoreCase("distance")) {
Log.i("Distance:", "Distance added to parser");
distance = readDistance(parser);
} else {
Log.i("Skip: ", "String Skipped");
skip(parser);
}
}
Log.i("Parser", "New System Added to be displayed");
return new System(systemName, rightAscension, declination, distance);
}
How would I change the code so that at the end of the code only one System is returned? Thank you! Here is more of my code, where the readSystem is called:
public List<System> parse(InputStream is) throws XmlPullParserException, IOException {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
Log.i("Parser", "Parser created");
try {
//factory = XmlPullParserFactory.newInstance();
// factory.setNamespaceAware(true);
// parser = factory.newPullParser();
parser.setInput(is, null);
parser.nextTag();
Log.i("System:", "readSystem called");
return readSystem(parser);
} finally {
is.close();
Log.i("InputStream", "InputStream Closed");
}
}
private List readSystem(XmlPullParser parser) throws XmlPullParserException, IOException {
List systems = new ArrayList();
parser.require(XmlPullParser.START_TAG, ns, "system");
Log.i("Parser", "Passed parser.require");
String name = parser.getName();
Log.i("parser", "getName() Called");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
Log.i("readSystem", "Start Tag Continue called");
continue;
}
if (name.equalsIgnoreCase("system")) {
Log.i("String", "System added to parser");
systems.add(readEntry(parser));
} else {
Log.i("String", "No system to add, skipped, no readEntry called");
skip(parser);
}
}
return systems;
}
Well there can be a lot of ending tags in an XML. For instance:
Here we have 3 ending tags. If we were parsing a foo element, then with your current code you'll stop parsing as soon as the
bar
ending tag is reached.You need to check the name of the node when the tag is an ending tag, and only if it is the system tag, finish the parsing.
If you only have a "System" element in the whole document, you can also use the
XmlPullParser.END_DOCUMENT
constant to check if you have finished the parsing. You have a good example here:http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html