Android的XML解析失败意外标记 [英] Android XML parse failed Unexpected token

查看:251
本文介绍了Android的XML解析失败意外标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序(游戏),我需要读取XML脚本,并在转成一个类的对象;我已经通过DOM完成了整个XML阅读器

但是当我跑,我得到了以下错误:

  05-08 23:03:22.342:我/的System.out(588):XML帕兴Excpetion = org.xml.sax.SAXParseException:意外令牌(位置:TEXT ... // @在java.io.InputStreamReader@4129a200 1:69)
 

我读过有关这方面的一些答案,但他们都没有解决我的问题(http://stackoverflow.com/questions/7885962/android-utf-8-file-parsing)..

这是我的code:

 的InputStream的InputStream = ctx.getResources()openRawResource(R.xml.script)。
。ctx.getApplicationContext()getPackageName()+/ XML /+路径);



        读卡器读卡器=新的InputStreamReader(InputStream中,UTF-8);

        InputSource的是=新的InputSource(读卡器);
        is.setEncoding(UTF-8);


        DocumentBuilderFactory的DBF = DocumentBuilderFactory.newInstance();
        DocumentBuilder的DB = dbf.newDocumentBuilder();


        文档根= db.parse(是);
 

根据这个问题,我也试着这:

 的InputStream的InputStream = ctx.getResources()openRawResource(R.xml.script)。
 文档根= db.parse(InputStream的);
 

而产生完全相同的例外...

如何解决这个问题呢?

我的XML文件:

 <脚本>

&所述;场景没有=0>
    <性格ID =1> 1< /字符>

    &所述;对话没有=1>
        <冠军> 1< /标题>
 子元素1
    < /对话框>
< /场景>



< / SCRIPT>
 

解决方案

您的XML是无效的。 子元素1应该是一个元素本身。 XML的文档也应该以一个标签将其定义为XML:<?code>&LT; XML版本=1.0&GT; 所以,你的整个文件是:

 &LT; XML版本=1.0&GT?;
&LT;脚本&GT;
&所述;场景没有=0&GT;
    &LT;性格ID =1&GT; 1&LT; /字符&GT;
    &所述;对话没有=1&GT;
        &LT;冠军&GT; 1&LT; /标题&GT;
        &LT; something_else&GT;子元素1 LT; / something_else&GT;
    &LT; /对话框&GT;
&LT; /场景&GT;
&LT; / SCRIPT&GT;
 

或者你可以使用子元素作为元素名(而不是 something_else )和值1。

In my app(game), i need to read a XML script and turn in into a class object; i've completed the entire XML reader via DOM

but when i run , i got the following error:

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) 

i've read some answers about this but they all failed to fix my problem (http://stackoverflow.com/questions/7885962/android-utf-8-file-parsing)..

here's my code:

    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);

according to the problem, i've also tried this to:

 InputStream inputStream = ctx.getResources().openRawResource(R.xml.script);
 Document root = db.parse(inputStream);

which produced exactly the same exceptions...

how to solve this problem?

my xml file:

<script>

<scene no="0">
    <character id="1">1</character>

    <dialog no="1">
        <title>1</title>
 Sub-Element 1
    </dialog>
</scene>



</script>

解决方案

Your XML is invalid. "Sub-Element 1" should be within an element itself. XML-documents should also begin with a tag defining it as XML: <?xml version="1.0" ?> So your complete file would be:

<?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>

Or you could use "Sub-Element" as the element name (instead of something_else) and 1 as the value.

这篇关于Android的XML解析失败意外标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆