存在声明时 XML Pull 解析器错误

发布于 2024-10-16 08:44:10 字数 1092 浏览 7 评论 0原文

我正在尝试使用 xml pull 解析器解析 xml 文档。 一切工作正常,直到我开始处理包含 xxml 声明的 xml 文档:

当声明存在时,我收到以下错误:

02-08 15:37:16.960: WARN/System.err(9721): org.xmlpull.v1 .XmlPullParserException: PI 不得以 xml 开头(位置:未知 @1:5 in java.io.InputStreamReader@47ec2770)

如果我从文档中取出声明,一切正常。 对我来说切换到另一个解析器已经太晚了,所以我需要让它工作!

这是我的解析器代码的样子

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(false);
    // factory.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, true);
    XmlPullParser xpp = factory.newPullParser();

    // get a reference to the file.

    File file = new File(Environment.getExternalStorageDirectory() + "/"
            + Constants.SD_CARD_DIR + "/" + Constants.XMLPATH);
    // create an input stream to be read by the stream reader.
    FileInputStream fis = new FileInputStream(file);
    // set the input for the parser using an InputStreamReader
    xpp.setInput(new InputStreamReader(fis));
    int eventType = xpp.getEventType();

    // /

    while (eventType != XmlPullParser.END_DOCUMENT) {...}

I'm trying to parse an xml document using the xml pull parser.
Everything worked fine until i started dealing with an xml document containing an xxml declartation:

When the declaration is there i get the following error:

02-08 15:37:16.960: WARN/System.err(9721): org.xmlpull.v1.XmlPullParserException: PI must not start with xml (position:unknown @1:5 in java.io.InputStreamReader@47ec2770)

If I take out the declaration from the document, everything works.
It's too late for me to switch to another parser so i need to make it work!

Here's what my parser code looks like

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(false);
    // factory.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, true);
    XmlPullParser xpp = factory.newPullParser();

    // get a reference to the file.

    File file = new File(Environment.getExternalStorageDirectory() + "/"
            + Constants.SD_CARD_DIR + "/" + Constants.XMLPATH);
    // create an input stream to be read by the stream reader.
    FileInputStream fis = new FileInputStream(file);
    // set the input for the parser using an InputStreamReader
    xpp.setInput(new InputStreamReader(fis));
    int eventType = xpp.getEventType();

    // /

    while (eventType != XmlPullParser.END_DOCUMENT) {...}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昇り龍 2024-10-23 08:44:10

It seems that your xml file starts with the UTF-8 byte-order-mark (see here). Probably happened when you copied the declaration. The solution depends on the editor you're using, some of them can be set not to write the BOM. Sometimes it disappears when you delete the first character of the file and type it again.

柠檬色的秋千 2024-10-23 08:44:10

我刚刚遇到了同样的问题,它实际上是由 utf-8 bom 编码引起的,但我无法解决它,因为 xml 文件是由服务器创建的,我无法修改它

I just had the same problem and it's actually caused by utf-8 bom encoding,but i could't solve it because the xml file was created by the server,i could't modify it

偏爱你一生 2024-10-23 08:44:10

我也遇到了同样的问题,但我发现这不是 XML 文档格式不正确的错误,有时会是这个问题,但这一次,这是由于没有使用服务器用来发送的正确输出流数据到客户端...
我的服务器端代码是这样的:

DataOutputStream dos = new DataOutputStream((OutputStream) response.getOutputStream());

但是我的客户端代码是:

InputStream is =(InputStream) httpConnection.openDataInputStream();

所以最后我更改了流以相互匹配,然后问题也解决了......
我仍然不明白为什么当这些流不同时会发生这种情况,即使这些流是由相同的InputStream继承的

(我将其用于J2ME sdk 3.0.5)

I also had the same problem but I figured out that it is not a fault of not well formed XML document ,Sometimes it would be that problem but this time, it is due by not using correct output stream that the server is using to send the data to the client side ...
my server-side code is this :

DataOutputStream dos = new DataOutputStream((OutputStream) response.getOutputStream());

but my client side code is :

InputStream is =(InputStream) httpConnection.openDataInputStream();

so finally I changed the streams to match each other ,then the problem also solved ...
I still don't understand why is it happens when those streams are different even the streams are inherited by the same InputStream

(I used this for J2ME sdk 3.0.5)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文