mardi 10 mars 2015

Why is XmlPullParser.getName() returning null?



While I am attempting to parse XML using the XmlPullParser the getName() method is returning null. I checked to make sure the inputstream picked up the file okay and it did. Here is my code:



public ArrayList<Card> parseXMLAndStoreIt(Context myContext) throws XmlPullParserException, IOException {
int event, end;
XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
xmlFactoryObject.setNamespaceAware(true);
XmlPullParser myParser = xmlFactoryObject.newPullParser();
ArrayList<Card> cardlist;
String text=null;
cardlist = new ArrayList<Card>();

try{
AssetManager mgr = myContext.getAssets();
InputStream is = mgr.open("cardlist.xml");
/* This was used to test if the xml was opened properly and the inputstream took it
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
while(s.hasNext()){
Log.i("Scanner", s.nextLine());
}
*/
myParser.setInput(is, null);
event = myParser.getEventType();

while(event != XmlPullParser.END_DOCUMENT){
Log.i("XMLParser", "Event Value: " + event);
Log.i("XMLParser", "Parser Name: " + myParser.getName());//Failing here, getName returning null
String name = myParser.getName();
end = cardlist.size() - 1;
Log.i("end", "" + cardlist.size());
switch (event){
case XmlPullParser.START_TAG:
cardlist.add(new Card());
Log.i("starttag", "start");
break;
case XmlPullParser.TEXT:
text = myParser.getText();
Log.i("textxml", "getText");
break;
case XmlPullParser.END_TAG:
if(name.equals("Class")){
cardlist.get(end).setHero(text);
Log.i("Classtest", name);
}else if(name.equals("Name")){
cardlist.get(end).setName(text);
}else if(name.equals("Ability")){
cardlist.get(end).setAbility(text);
}else if(name.equals("Mana")){
cardlist.get(end).setManacost(Integer.parseInt(text));
}else if(name.equals("Attack")){
cardlist.get(end).setAttack(Integer.parseInt(text));
}else if(name.equals("Health")){
cardlist.get(end).setHealth(Integer.parseInt(text));
}
break;
default:
break;
}
event = myParser.next();
}
}catch (Exception e){
Log.e("XMLParser", "Failure to parse XML data");
}
return cardlist;
}


Here is a snippet of the xml I am reading:



<Classes>
<Druid>
<Card>
<Index>14</Index>
<Name>Ancient of Lore</Name>
<Class>Druid</Class>
<Rarity>Epic</Rarity>
<Type>Minion</Type>
<Race/>
<Mana>7</Mana>
<Attack>5</Attack>
<Health>5</Health>
<Set>Expert</Set>
<Ability>Choose One - Draw 2 cards; or Restore 5 Health.</Ability>
</Card>
</Druid>
</Classes>



Aucun commentaire:

Enregistrer un commentaire