关于问题可能是什么我没有想法..我在Android上使用SimpleXML,它抛出了以下堆栈跟踪:
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
从以下XML文件加载:
<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>
并使用以下POJO作为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;
}
}
加载发生在这里:
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();
}
}
我也尝试使用InputStream,但我得到了同样的错误.
我已经搜索了答案,但它总是在XML中没有被解决.我甚至没有任何应该被转义的角色.
我甚至检查了抛出异常的xml解析器的源代码,但它是相当无用的:
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 }
我不知道是什么!放松意味着什么.因此,我完全没有关于如何解决这个问题的想法.
有没有人遇到过这个?我能做错什么?我只是看不出任何错误,即使我的XML有效.我想我也正确地注释了这些课程.有任何想法吗?
编辑:显然我的调试感觉刺痛,我通过System.out发送bufferedReader的内容.
结果:
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??????????????????????
我将试图找出为什么我从流中读取的XML是完全乱码….
解决方法:
我找到了解决方案,显然我的调试意识设法让我朝着正确的方向前进.
显然是用数据读取数据
br = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsstream("res/xml/levels.xml")));
是一个可怕的想法.解决方案是将XML文件放入/ res / raw文件夹,并将其作为原始资源打开.
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);
}
}
我希望这将有助于将来的某个人,并希望不会因为我在20分钟后解决的问题而堵塞SO:D
编辑:
另外,我需要补充一下
@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,否则根据我的XML结构无法找到数据.但这与这个问题没有关系.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。