Saturday, October 1, 2011

[android-developers] Re: XML DOM Parser works in JAVA, Crashes Android App, Now XMLPullParser

I figured it out. It was a combination of not accessing the correct
Android resources and faulty data structure. The correct code follows
(hope this helps someone else):


public void parseCharXmlFile(){
Chars myCh;
String nameTag= "";

try {
Resources res = getResources();
int eventType = parser.getEventType();
boolean done = false;

while (eventType != XmlPullParser.END_DOCUMENT && !done ){
switch (eventType){
case XmlPullParser.START_DOCUMENT:
chars.clear();
break;
case XmlPullParser.START_TAG:
nameTag = parser.getName();
System.out.println("Reading start tag " + nameTag);
if (nameTag.equalsIgnoreCase("ch") ) {
myCh = new Chars(' ',0);
myCh.setTheChar(parser.nextText().charAt(1));
} else if (nameTag.equalsIgnoreCase("count")) {
myCh.setCount(Integer.parseInt(parser.nextText()));
}
break;
case XmlPullParser.END_TAG:
nameTag = parser.getName();
if (nameTag.equalsIgnoreCase("Chars") ){
done = true;
} else if (nameTag.equalsIgnoreCase("ChPair"))
{
chars.add(myCh);
}
break;
} // event switch end
eventType = parser.next();
} // end while
} catch (XmlPullParserException ex) {
System.out.println(ex);
} catch (IOException e) {
System.out.println(e);
}
}

Chris

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment