java - SimpleXML throws XmlPullParserException, unterminated entity ref for no reason -
i ran out of ideas on problem be.. i'm using simplexml on android, , threw following stacktrace:
06-08 13:20:56.450: e/androidruntime(2281): fatal exception: main 06-08 13:20:56.450: e/androidruntime(2281): java.lang.runtimeexception: org.xmlpull.v1.xmlpullparserexception: unterminated entity ref (position:text ????t???????????????...@21:133 in java.io.bufferedreader@b6438630) 06-08 13:20:56.450: e/androidruntime(2281): @ com.example.stuff.manager.levelmanager.<init>(levelmanager.java:32) 06-08 13:20:56.450: e/androidruntime(2281): @ com.example.stuff.fragment.mainmenufragment.onclick(mainmenufragment.java:138) 06-08 13:20:56.450: e/androidruntime(2281): @ android.view.view.performclick(view.java:2485) 06-08 13:20:56.450: e/androidruntime(2281): @ android.view.view$performclick.run(view.java:9080) 06-08 13:20:56.450: e/androidruntime(2281): @ android.os.handler.handlecallback(handler.java:587) 06-08 13:20:56.450: e/androidruntime(2281): @ android.os.handler.dispatchmessage(handler.java:92) 06-08 13:20:56.450: e/androidruntime(2281): @ android.os.looper.loop(looper.java:130) 06-08 13:20:56.450: e/androidruntime(2281): @ android.app.activitythread.main(activitythread.java:3683) 06-08 13:20:56.450: e/androidruntime(2281): @ java.lang.reflect.method.invokenative(native method) 06-08 13:20:56.450: e/androidruntime(2281): @ java.lang.reflect.method.invoke(method.java:507) 06-08 13:20:56.450: e/androidruntime(2281): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 06-08 13:20:56.450: e/androidruntime(2281): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 06-08 13:20:56.450: e/androidruntime(2281): @ dalvik.system.nativestart.main(native method) 06-08 13:20:56.450: e/androidruntime(2281): caused by: org.xmlpull.v1.xmlpullparserexception: unterminated entity ref (position:text ????t???????????????...@21:133 in java.io.bufferedreader@b6438630) 06-08 13:20:56.450: e/androidruntime(2281): @ org.kxml2.io.kxmlparser.exception(kxmlparser.java:273) 06-08 13:20:56.450: e/androidruntime(2281): @ org.kxml2.io.kxmlparser.error(kxmlparser.java:269) 06-08 13:20:56.450: e/androidruntime(2281): @ org.kxml2.io.kxmlparser.pushentity(kxmlparser.java:781) 06-08 13:20:56.450: e/androidruntime(2281): @ org.kxml2.io.kxmlparser.pushtext(kxmlparser.java:849) 06-08 13:20:56.450: e/androidruntime(2281): @ org.kxml2.io.kxmlparser.nextimpl(kxmlparser.java:354) 06-08 13:20:56.450: e/androidruntime(2281): @ org.kxml2.io.kxmlparser.next(kxmlparser.java:1378) 06-08 13:20:56.450: e/androidruntime(2281): @ org.simpleframework.xml.stream.pullreader.read(pullreader.java:105) 06-08 13:20:56.450: e/androidruntime(2281): @ org.simpleframework.xml.stream.pullreader.next(pullreader.java:89) 06-08 13:20:56.450: e/androidruntime(2281): @ org.simpleframework.xml.stream.nodereader.readelement(nodereader.java:111) 06-08 13:20:56.450: e/androidruntime(2281): @ org.simpleframework.xml.stream.nodereader.readroot(nodereader.java:85) 06-08 13:20:56.450: e/androidruntime(2281): @ org.simpleframework.xml.stream.nodebuilder.read(nodebuilder.java:84) 06-08 13:20:56.450: e/androidruntime(2281): @ org.simpleframework.xml.stream.nodebuilder.read(nodebuilder.java:71) 06-08 13:20:56.450: e/androidruntime(2281): @ org.simpleframework.xml.core.persister.read(persister.java:562) 06-08 13:20:56.450: e/androidruntime(2281): @ org.simpleframework.xml.core.persister.read(persister.java:462) 06-08 13:20:56.450: e/androidruntime(2281): @ com.example.stuff.manager.levelmanager.<init>(levelmanager.java:27) 06-08 13:20:56.450: e/androidruntime(2281): ... 12 more
loading following xml file:
<levels> <level id="1" time="400"> <fruitdatas> <fruitdata id="2" row="1" column="1"></fruitdata> <fruitdata id="0" row="1" column="2"></fruitdata> <fruitdata id="3" row="1" column="3"></fruitdata> <fruitdata id="4" row="1" column="4"></fruitdata> </fruitdatas> </level> </levels>
and following pojos made containers stuff in xml:
fruitdata.java:
@root public class fruitdata { @attribute private integer id; @attribute private integer row; @attribute private integer column; public integer getid() { return id; } public void setid(integer id) { this.id = id; } public int getrow() { return row; } public void setrow(int row) { this.row = row; } public int getcolumn() { return column; } public void setcolumn(int column) { this.column = column; } }
fruitdatas.java:
@root public class fruitdatas { @elementlist private list<fruitdata> fruitdatas; public fruitdatas() { } public fruitdatas(@elementlist(name = "fruitdatas") list<fruitdata> fruitdatas) { this.fruitdatas = fruitdatas; } public list<fruitdata> getfruitdatas() { return fruitdatas; } public void setfruitdatas(list<fruitdata> fruitdatas) { this.fruitdatas = fruitdatas; } }
level.java:
@root public class level { @attribute private long id; @attribute private long time; @elementlist private list<fruitdata> fruitdatas; public long getid() { return id; } public void setid(long id) { this.id = id; } public long gettime() { return time; } public void settime(long time) { this.time = time; } public list<fruitdata> getfruitdatas() { return fruitdatas; } public void setfruitdatas(list<fruitdata> fruits) { this.fruitdatas = fruits; } }
levels.java:
@root public class levels { @elementlist private list<level> levels; public levels() { } public levels(@elementlist(name = "levels") list<level> levels) { this.levels = levels; } public list<level> getlevels() { return levels; } public void setlevels(list<level> levels) { this.levels = levels; } }
the loading happens here:
public levelmanager() { serializer serializer = new persister(); levels levels = null; bufferedreader br = null; try { br = new bufferedreader(new inputstreamreader(thread.currentthread().getcontextclassloader().getresourceasstream("res/xml/levels.xml"))); levels = serializer.read(levels.class, br); } catch (exception e) { log.e(getclass().getsimplename(), "error in deserialization.", e); throw new runtimeexception(e); } { if (br != null) { try { br.close(); } catch (ioexception e) { } } } if (levels != null) { this.levels = levels.getlevels(); } }
i tried using inputstream, i'm getting same error. i've searched answers, being unescaped in xml. don't have characters supposed escaped.
i checked source code of xml parser exception thrown, unhelpful:
723 private final void more ...pushentity() 724 throws ioexception, xmlpullparserexception { 725 726 push(read()); // & 727 728 729 int pos = txtpos; 730 731 while (true) { 732 int c = read(); 733 if (c == ';') 734 break; 735 if (c < 128 736 && (c < '0' || c > '9') 737 && (c < 'a' || c > 'z') 738 && (c < 'a' || c > 'z') 739 && c != '_' 740 && c != '-' 741 && c != '#') { 742 if(!relaxed){ 743 error("unterminated entity ref"); 744 } 745 //; ends with:"+(char)c); 746 if (c != -1) 747 push(c); 748 return; 749 } 750 751 push(c); 752 } 753 754 string code = get(pos); 755 txtpos = pos - 1; 756 if (token && type == entity_ref){ 757 name = code; 758 } 759 760 if (code.charat(0) == '#') { 761 int c = 762 (code.charat(1) == 'x' 763 ? integer.parseint(code.substring(2), 16) 764 : integer.parseint(code.substring(1))); 765 push(c); 766 return; 767 } 768 769 string result = (string) entitymap.get(code); 770 771 unresolved = result == null; 772 773 if (unresolved) { 774 if (!token) 775 error("unresolved: &" + code + ";"); 776 } 777 else { 778 (int = 0; < result.length(); i++) 779 push(result.charat(i)); 780 } 781 }
i have no idea !relaxed means. therefore, ran out of ideas on how fix problem.
has ran before? doing wrong? don't see wrong, xml valid. think annotated classes properly, too. ideas?
edit: apparently debug senses tingling, , sent bufferedreader's content through system.out.
result:
06-08 13:47:51.830: i/system.out(2334): ????t???????????????????????????????????h?????????????????????? ????????????????????????!??????'??????4??????@??????d??????j??????s??????w??????[??????_??????c??????g??????k??????o??????levels??id??time??level??1??400?? 06-08 13:47:51.830: i/system.out(2334): fruitdatas?? fruitdata??0??row??column??2??4??3??5??6??7??8??9??????$??????????????????????????????????????????????????l???????????????????????????????????????????????????????????????????????????????????????????????$????????????????????????????????????????????????$????????????????????????????????????????????????$??????????????????????????????????????????????????????????????????????????????????????????????????????????????????$???????????????????? ?????????????????????????????????????????????????????????????????????????????????????? ????????$???????????????????? 06-08 13:47:51.830: i/system.out(2334): ?????????????????????????????????????????????????????????????????????????????????????? 06-08 13:47:51.830: i/system.out(2334): ?????????????? ??????????????????????$?????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????????????$??????????????????????????????????????????????????????????????????????????????????????????????????????????????????$???????????????????? ?????????????????????????????????????????????????????????????????????????????????????? ????????$?????? 06-08 13:47:51.830: i/system.out(2334): ?????????????? 06-08 13:47:51.830: i/system.out(2334): ?????????????????????????????????? 06-08 13:47:51.830: i/system.out(2334): ?????????????????????????????????????? 06-08 13:47:51.830: i/system.out(2334): ?????????????? 06-08 13:47:51.830: i/system.out(2334): ????????????????????????????????????$????????????????????????????????????????????????$??????????????????????????????????????????????????????????????????????????????????????????????????????????????????$???????????????????? ?????????????????????????????????????????????????????????????????????????????????????? ????????$???????????????????? 06-08 13:47:51.830: i/system.out(2334): ?????????????????????????????????????????????????????????????????????????????????????? 06-08 13:47:51.830: i/system.out(2334): ????????????????????????????????????$????????????????????????????????????????????????$??????????????????????????????????????????????????????????????????????????????????????????????????????????????????$???????????????????? ?????????????????????????????????????????????????????????????????????????????????????? ????????$???????????????????? 06-08 13:47:51.830: i/system.out(2334): ???????????????????????????????????????????? 06-08 13:47:51.830: i/system.out(2334): ?????????????????????????????????????????? 06-08 13:47:51.830: i/system.out(2334): ????????????????????????????????????$????????????????????????????????????????????????$??????????????????????????????????????????????????????????????????????????????????????????????????????????????????$???????????????????? ?????????????????????????????????????????????????????????????????????????????????????? ????????$???????????????????? 06-08 13:47:51.830: i/system.out(2334): ?????????????????????????????????????????????????????????????????????????????????????? 06-08 13:47:51.830: i/system.out(2334): ????????????????????????????????????$????????????????????????????????????????????????$??????????????????????????????????????????????????????????????????????????????????????????????????????????????????$?????? ?????????????? ?????????????????????????????????? ?????????????????????????????????????? ?????????????? ????????$??????!?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????!??????????????????????????????????????!?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????"??????????????????????$??????#??????????????????????????????????????????$??????$????????????????????????????????????????????????$?????????? 06-08 13:47:51.830: i/system.out(2334): ????????????????????????????$??????????????????????$??????%?????????????? ??????????????????????????????????%??????????????????????????????????????%?????????????? ????????$??????&?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????&??????????????????????????????????????&?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????'??????????????????????$??????(??????????????????????????????????????????$??????)????????????????????????????????????????????????)??????????????????????????????????????)??????????????????????$??????*?????????????? ??????????????????????????????????*??????????????????????????????????????*?????????????? ????????$??????+?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????+??????????????????????????????????????+?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????,??????????????????????$??????-??????????????????????????????????????????$??????.????????????????????????????????????????????????.??????????????????????????????????????.??????????????????????$??????/?????????????? ??????????????????????????????????/??????????????????????????????????????/?????????????? ????????$??????0?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????0??????????????????????????????????????0?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????1??????????????????????$??????2??????????????????????????????????????????$??????3????????????????????????????????????????????????3??????????????????????????????????????3??????????????????????$??????4?????????????? ??????????????????????????????????4??????????????????????????????????????4?????????????? ????????$??????5?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????5??????????????????????????????????????5?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????6??????????????????????$??????7??????????????????????????????????????????$??????8????????????????????????????????????????????????8??????????????????????????????????????8??????????????????????$??????9?????????????? ??????????????????????????????????9??????????????????????????????????????9?????????????? ????????$??????:?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????:??????????????????????????????????????:?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????;??????????????????????$??????<??????????????????????????????????????????$??????=????????????????????????????????????????????????=??????????????????????????????????????=??????????????????????$??????>?????????????? ??????????????????????????????????>??????????????????????????????????????>?????????????? ????????$????????????????????? 06-08 13:47:51.830: i/system.out(2334): ???????????????????????????????????????????????????????????????????????????????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????@??????????????????????$??????a??????????????????????????????????????????$??????b????????????????????????????????????????????????b??????????????????????????????????????b??????????????????????$??????c?????????????? ??????????????????????????????????c??????????????????????????????????????c?????????????? ????????$??????d?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????d??????????????????????????????????????d?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????e??????????????????????$??????f??????????????????????????????????????????$??????g????????????????????????????????????????????????g?????????? 06-08 13:47:51.830: i/system.out(2334): ????????????????????????????g??????????????????????$??????h?????????????? ??????????????????????????????????h??????????????????????????????????????h?????????????? ????????$??????i?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????i??????????????????????????????????????i?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????j??????????????????????$??????k??????????????????????????????????????????$??????l????????????????????????????????????????????????l??????????????????????????????????????l??????????????????????$??????m?????????????? ??????????????????????????????????m??????????????????????????????????????m?????????????? ????????$??????n?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????n?????????? 06-08 13:47:51.830: i/system.out(2334): ????????????????????????????n?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????o??????????????????????$??????p??????????????????????????????????????????$??????q????????????????????????????????????????????????q??????????????????????????????????????q??????????????????????$??????r?????????????? ??????????????????????????????????r??????????????????????????????????????r?????????????? ????????$??????s?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????s??????????????????????????????????????s?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????t??????????????????????$??????u??????????????????????????????????????????$??????v????????????????????????????????????????????????v??????????????????????????????????????v??????????????????????$??????w?????????????? ??????????????????????????????????w??????????????????????????????????????w?????????????? ????????$??????x?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????x??????????????????????????????????????x?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????y??????????????????????$??????z??????????????????????????????????????????$??????[????????????????????????????????????????????????[?????????? 06-08 13:47:51.830: i/system.out(2334): ????????????????????????????[??????????????????????$??????\?????????????? ??????????????????????????????????\??????????????????????????????????????\?????????????? ????????$??????]?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????]??????????????????????????????????????]?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????^??????????????????????$??????_??????????????????????????????????????????$??????`????????????????????????????????????????????????`??????????????????????????????????????`??????????????????????$??????a?????????????? ??????????????????????????????????a??????????????????????????????????????a?????????????? ????????$??????b?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????b??????????????????????????????????????b?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????c??????????????????????$??????d??????????????????????????????????????????$??????e????????????????????????????????????????????????e??????????????????????????????????????e??????????????????????$??????f?????????????? ??????????????????????????????????f??????????????????????????????????????f?????????????? ????????$??????g?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????g??????????????????????????????????????g?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????h??????????????????????$??????i??????????????????????????????????????????$??????j????????????????????????????????????????????????j?????????? 06-08 13:47:51.830: i/system.out(2334): ????????????????????????????j??????????????????????$??????k?????????????? ??????????????????????????????????k??????????????????????????????????????k?????????????? ????????$??????l?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????????????????????????l??????????????????????????????????????l?????????????? 06-08 13:47:51.830: i/system.out(2334): ??????????????m????????????????????????????n????????????????????????????o????????????????????????????p??????????????????????
i'll attempt figure out why xml read in stream total gibberish....
i have found solution, apparently debug senses managed me in right direction.
so apparently reading data
br = new bufferedreader(new inputstreamreader(thread.currentthread().getcontextclassloader().getresourceasstream("res/xml/levels.xml")));
was terrible idea. solution put xml files /res/raw folder, , open them raw resource.
public levelmanager(context context) { serializer serializer = new persister(); levels levels = null; bufferedreader br = null; try { br = new bufferedreader(new inputstreamreader(context.getresources().openrawresource(r.raw.levels))); levels = serializer.read(levels.class, br); } catch (exception e) { log.e(getclass().getsimplename(), "error in deserialization.", e); throw new runtimeexception(e); } { if (br != null) { ioutils.closequietly(br); } }
this way, content of file read.
i hope in future, , not clogging question resolved after 20 minutes :d
edit: also, needed add
@root public class levels { @elementlist(type=level.class, inline=true) private list<level> levels; public levels() { } public levels(@elementlist(name = "levels", type=level.class, inline=true) list<level> levels) { this.levels = levels; } public list<level> getlevels() { return levels; } public void setlevels(list<level> levels) { this.levels = levels; } }
inline=true because otherwise couldn't find data according xml structure. wasn't related issue.
Comments
Post a Comment