java - Why does my XML parser not work -
i got following xml file named test2.xml in assets folder:
<?xml version="1.0" encoding="utf-8"?> <map version="1.0" orientation="orthogonal" width="32" height="32" tilewidth="16" tileheight="16"> <tileset firstgid="1" name="terrain" tilewidth="16" tileheight="16"> <image source="terrain.png" width="256" height="256"/> </tileset> <layer name="kachelebene 2" width="32" height="32"> <data encoding="csv"> 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,187,187,187,187,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,164,164,164,164,164,164,164,187,187,164,164,187,187,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,164,0,187,187,187,164,164,164,164,187,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 </data> </layer> <layer name="kachelebene 1" width="32" height="32"> <data encoding="csv"> 3,3,3,164,3,164,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3,3,3,36,36,36,36,36,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,36,36,36,3,3,36,36,36,164,36,36,36,36,36,36,36,36,36,36,36,36,3,3,3,3, 3,3,3,3,3,3,3,3,3,36,36,36,3,3,3,164,164,3,3,3,3,3,3,3,3,3,3,3,36,36,36,3, 3,3,3,3,3,3,3,3,36,36,3,3,3,187,164,164,164,164,3,3,3,3,3,3,3,3,3,3,3,3,36,36, 3,3,3,3,3,3,3,36,36,3,3,3,3,3,164,164,187,164,164,164,164,164,3,3,164,164,3,3,3,3,3,3, 3,3,3,3,3,3,36,36,3,3,3,3,3,3,3,164,164,164,164,164,164,164,164,164,164,3,3,3,3,3,3,3, 3,3,3,3,3,3,36,3,3,3,3,3,3,3,3,164,3,164,164,164,164,164,164,3,3,3,3,3,3,3,3,3, 3,3,3,3,3,36,36,3,3,3,3,3,3,3,3,164,164,164,164,164,164,164,164,164,164,164,164,3,3,3,3,3, 3,3,3,3,3,36,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, 3,3,3,3,36,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,3,4,4,4,3,3,3, 3,3,3,36,36,3,3,3,4,4,4,4,4,4,4,4,4,4,207,207,207,207,207,207,207,3,3,3,3,4,3,3, 3,3,36,36,3,3,3,3,4,3,71,207,207,207,207,207,207,207,207,207,207,207,207,207,3,71,3,3,3,4,3,3, 3,36,36,3,3,3,3,3,4,3,71,207,207,207,207,207,207,207,207,207,207,207,3,207,3,71,3,3,3,3,3,3, 3,36,3,3,3,3,3,3,4,3,71,207,207,207,207,207,207,207,207,207,3,3,207,207,3,71,3,3,3,4,4,3, 36,36,3,3,3,3,3,3,4,3,71,207,207,207,207,207,207,207,207,3,207,207,207,207,3,71,3,3,3,3,4,3, 36,3,3,3,3,3,3,3,4,3,71,207,207,207,207,207,207,207,207,207,207,3,207,3,3,71,3,3,3,3,4,3, 36,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,3, 36,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, 36,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3,3, 36,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3, 36,3,3,3,3,3,3,3,36,36,36,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3, 36,3,3,3,3,3,36,36,36,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3, 36,36,36,36,36,36,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3 </data> </layer> </map>
and tried read following class:
package de.anlixstudios.davidsdragonadventure; import java.io.ioexception; import java.io.inputstream; import java.util.hashmap; import java.util.list; import org.xmlpull.v1.xmlpullparser; import org.xmlpull.v1.xmlpullparserexception; import org.xmlpull.v1.xmlpullparserfactory; import android.content.context; import android.util.log; import android.widget.toast; public class levelcreator { public list<hashmap<integer, list<integer>>> levels = null; public list<string> levelname = null; private graphicsholder mgraphicsholder; public static final string ns = null; private gameactivity mgameactivity; private string heightstring; private string widthstring; private inputstream in_s; private context context; private int height = 0; private int width = 0; public levelcreator(context context, graphicsholder holder){ // todo adding parameters gameactivity , graphicsholder this.context = context; mgameactivity = (gameactivity) context; mgraphicsholder = holder; log.d("levelcreator", "created"); } // public accessible function, returns levels public list<hashmap<integer, list<integer>>> getlevels(){ xmlpullparserfactory pullparserfactory; try{ pullparserfactory = xmlpullparserfactory.newinstance(); log.d("pullparserfactory", "new instance created"); xmlpullparser parser = pullparserfactory.newpullparser(); in_s = context.getassets().open("test2.xml"); log.d("got assets", "setting input , starting"); parser.setfeature(xmlpullparser.feature_process_namespaces, false); parser.setinput(in_s, null); parser.nexttag(); return parsexml(parser); } catch (xmlpullparserexception e){ e.printstacktrace(); } catch (ioexception e){ e.printstacktrace(); } return null; } // called public function , actual work private list<hashmap<integer, list<integer>>> parsexml(xmlpullparser parser) throws xmlpullparserexception,ioexception { log.d("levelcreator", "parsexml"); list<hashmap<integer, list<integer>>> field = null; parser.require(xmlpullparser.start_tag, ns, "map"); int eventtype = parser.geteventtype(); while(eventtype != xmlpullparser.end_document){ log.d("levelcreator", "eventtype != xmlpullparser.end_document"); if(parser.geteventtype() != xmlpullparser.start_tag) { continue; } string name = parser.getname(); log.d("levelcreator", "name: " + name); // starts looking entry tag if(name.equals("layer")) { log.d("levelcreator", "name = layer"); widthstring = (string) parser.getproperty("width"); heightstring = (string) parser.getproperty("height"); try { width = integer.parseint(widthstring); height = integer.parseint(heightstring); } catch(numberformatexception nfe) { system.out.println("could not parse " + nfe); } log.d("width", widthstring); log.d("height", heightstring); field.add(readentry(parser)); } else { skip(parser); log.d("levelcreator", "skipped"); } } return field; } // returns 1 level int xml file private hashmap<integer, list<integer>> readentry(xmlpullparser parser) throws xmlpullparserexception, ioexception { parser.require(xmlpullparser.start_tag, ns, "layer"); hashmap<integer, list<integer>> data = null; while(parser.next() != xmlpullparser.end_tag) { if(parser.geteventtype() != xmlpullparser.start_tag) { continue; } string name = parser.getname(); if(name.equals("data")) { log.d("levelcreator", "name = data"); data = readdata(parser); } else { skip(parser); } } return data; } private hashmap<integer, list<integer>> readdata(xmlpullparser parser) throws xmlpullparserexception, ioexception { parser.require(xmlpullparser.start_tag, ns, "data"); string datastring = readtext(parser); log.d("levelcreator", "readtext"); parser.require(xmlpullparser.end_tag, ns, "data"); // todo make string hashmap return createmap(datastring); } // method skip tags not wanted private void skip(xmlpullparser parser) throws xmlpullparserexception, ioexception { log.d("levelcreator", "eventtype != xmlpullparser.end_document"); if (parser.geteventtype() != xmlpullparser.start_tag) { throw new illegalstateexception(); } int depth = 1; while (depth != 0) { switch (parser.next()) { case xmlpullparser.end_tag: depth--; break; case xmlpullparser.start_tag: depth++; break; } } } // returns text in <data></data> field private string readtext(xmlpullparser parser) throws ioexception, xmlpullparserexception { string result = ""; if (parser.next() == xmlpullparser.text) { result = parser.gettext(); parser.nexttag(); } return result; } // returns nothing should later return hashmap<integer, // list<integer>> string, info height , width private hashmap<integer, list<integer>> createmap(string xmldata) { log.d("levelcreator", "createmap"); hashmap<integer, list<integer>> level; list<integer> row; string[] alldata = xmldata.split(","); int count = 0; (string data : alldata) { log.d("" + count, data); } // todo making splitted string hashmap width , height return null; } public list<string> getlevelnames(){ return levelname; } }
parts of class example @ developer.android.com, thought understand seem not to. ah, , it's stuck @ logtext right before
if(eventtype != xmlpullparser.end_document){ continue; }
query. why? , have string data? ah, haven't written algorithm creating hashmap yet, because wanted see if { "0", "0", "0", ... } or { "0,", "0,", "0,", ... } back. nice if me. thx! oh , seem not have understood xmlparser anyway, if there easier way, please tell me :)
it's blocking because don't move in parser. need call next() or nexttag()
while(eventtype != xmlpullparser.end_document){ log.d("levelcreator", "eventtype != xmlpullparser.end_document"); eventtype = parser.next(); if(eventtype != xmlpullparser.start_tag) { continue; } ... }
that should help
Comments
Post a Comment