问题
I ran out of ideas on what the problem could be.. I'm using SimpleXML on Android, and it threw the 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): at com.example.stuff.manager.LevelManager.<init>(LevelManager.java:32)
06-08 13:20:56.450: E/AndroidRuntime(2281): at com.example.stuff.fragment.MainMenuFragment.onClick(MainMenuFragment.java:138)
06-08 13:20:56.450: E/AndroidRuntime(2281): at android.view.View.performClick(View.java:2485)
06-08 13:20:56.450: E/AndroidRuntime(2281): at android.view.View$PerformClick.run(View.java:9080)
06-08 13:20:56.450: E/AndroidRuntime(2281): at android.os.Handler.handleCallback(Handler.java:587)
06-08 13:20:56.450: E/AndroidRuntime(2281): at android.os.Handler.dispatchMessage(Handler.java:92)
06-08 13:20:56.450: E/AndroidRuntime(2281): at android.os.Looper.loop(Looper.java:130)
06-08 13:20:56.450: E/AndroidRuntime(2281): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-08 13:20:56.450: E/AndroidRuntime(2281): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 13:20:56.450: E/AndroidRuntime(2281): at java.lang.reflect.Method.invoke(Method.java:507)
06-08 13:20:56.450: E/AndroidRuntime(2281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-08 13:20:56.450: E/AndroidRuntime(2281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-08 13:20:56.450: E/AndroidRuntime(2281): at 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): at org.kxml2.io.KXmlParser.exception(KXmlParser.java:273)
06-08 13:20:56.450: E/AndroidRuntime(2281): at org.kxml2.io.KXmlParser.error(KXmlParser.java:269)
06-08 13:20:56.450: E/AndroidRuntime(2281): at org.kxml2.io.KXmlParser.pushEntity(KXmlParser.java:781)
06-08 13:20:56.450: E/AndroidRuntime(2281): at org.kxml2.io.KXmlParser.pushText(KXmlParser.java:849)
06-08 13:20:56.450: E/AndroidRuntime(2281): at org.kxml2.io.KXmlParser.nextImpl(KXmlParser.java:354)
06-08 13:20:56.450: E/AndroidRuntime(2281): at org.kxml2.io.KXmlParser.next(KXmlParser.java:1378)
06-08 13:20:56.450: E/AndroidRuntime(2281): at org.simpleframework.xml.stream.PullReader.read(PullReader.java:105)
06-08 13:20:56.450: E/AndroidRuntime(2281): at org.simpleframework.xml.stream.PullReader.next(PullReader.java:89)
06-08 13:20:56.450: E/AndroidRuntime(2281): at org.simpleframework.xml.stream.NodeReader.readElement(NodeReader.java:111)
06-08 13:20:56.450: E/AndroidRuntime(2281): at org.simpleframework.xml.stream.NodeReader.readRoot(NodeReader.java:85)
06-08 13:20:56.450: E/AndroidRuntime(2281): at org.simpleframework.xml.stream.NodeBuilder.read(NodeBuilder.java:84)
06-08 13:20:56.450: E/AndroidRuntime(2281): at org.simpleframework.xml.stream.NodeBuilder.read(NodeBuilder.java:71)
06-08 13:20:56.450: E/AndroidRuntime(2281): at org.simpleframework.xml.core.Persister.read(Persister.java:562)
06-08 13:20:56.450: E/AndroidRuntime(2281): at org.simpleframework.xml.core.Persister.read(Persister.java:462)
06-08 13:20:56.450: E/AndroidRuntime(2281): at com.example.stuff.manager.LevelManager.<init>(LevelManager.java:27)
06-08 13:20:56.450: E/AndroidRuntime(2281): ... 12 more
Loading from the 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 with the following POJOs I made as Containers for the stuff in the 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);
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (IOException e)
{
}
}
}
if (levels != null)
{
this.levels = levels.getLevels();
}
}
I also tried it using just the InputStream, but I'm getting the same error. I've searched for answers, but it was always something being unescaped in the XML. I don't even HAVE any characters that are supposed to be escaped.
I even checked the source code of the xml parser where the exception is thrown, but it was fairly 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 for (int i = 0; i < result.length(); i++)
779 push(result.charAt(i));
780 }
781 }
I have no idea what !relaxed means. Therefore, I completely ran out of ideas on how to fix this problem.
Has anyone ran into this before? What could I be doing wrong? I just don't see anything wrong, even my XML is valid. I think I annotated the classes properly, too. Any ideas?
EDIT: Apparently my debug senses were tingling, and I sent the 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 to figure out why the XML I read in from the stream was total gibberish....
回答1:
I have found the solution, apparently my debug senses managed to get me in the right direction.
So apparently reading the data with
br = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("res/xml/levels.xml")));
was a Terrible idea. The solution was to put the XML files into the /res/raw folder, and open them as 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);
}
finally
{
if (br != null)
{
IOUtils.closeQuietly(br);
}
}
This way, the content of the file was properly read.
I hope this will help someone in the future, and hopefully not just clogging up SO with a question I resolved after 20 minutes :D
EDIT: Also, I needed to 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 it couldn't find the data according to my XML structure. But that wasn't really related to the issue.
来源:https://stackoverflow.com/questions/24106734/simplexml-throws-xmlpullparserexception-unterminated-entity-ref-for-no-reason