在我的应用程序(游戏)中,我需要读取一个XML脚本并转换为一个类对象;我已经通过DOM完成了整个XML阅读器
但是当我运行时,我得到了以下错误:
05-08 23:03:22.342: I/System.out(588): XML Pasing Excpetion = org.xml.sax.SAXParseException: Unexpected token (position:TEXT ��������������������...@1:69 in java.io.InputStreamReader@4129a200) 我已经读过一些关于这个问题的答案,但是他们都没能解决我的问题(http://stackoverflow.com/questions/7885962/android-utf-8-file-parsing)。
下面是我的代码:
InputStream inputStream = ctx.getResources().openRawResource(R.xml.script);
ctx.getApplicationContext().getPackageName()+"/xml/"+path);
Reader reader = new InputStreamReader(inputStream,"UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document root = db.parse(is);根据这个问题,我也试着这样做:
InputStream inputStream = ctx.getResources().openRawResource(R.xml.script);
Document root = db.parse(inputStream);它产生了完全相同的例外情况...
如何解决这个问题?
我的xml文件:
<script>
<scene no="0">
<character id="1">1</character>
<dialog no="1">
<title>1</title>
Sub-Element 1
</dialog>
</scene>
</script>发布于 2012-05-08 23:19:55
您的XML无效。“子元素1”应该在元素本身内。XML-documents还应该以一个标记开始,该标记将其定义为XML:<?xml version="1.0" ?>,因此您的完整文件将是:
<?xml version="1.0" ?>
<script>
<scene no="0">
<character id="1">1</character>
<dialog no="1">
<title>1</title>
<something_else>Sub-Element 1</something_else>
</dialog>
</scene>
</script>或者,您可以使用"Sub- element“作为元素名称(而不是something_else),并使用1作为值。
发布于 2013-02-18 21:18:47
您将xml文件存储在哪里?在"res“文件夹中。您应该将其存储在"assets“中。Res-solution不能工作,因为特定的"res“文件夹和它在apk中的表现。将您的文件移动到"assets“,您将看到一切都在工作。使用以下代码创建InputSource
getAssets().open("yourfilename.xml")https://stackoverflow.com/questions/10501426
复制相似问题