存在声明时 XML Pull 解析器错误
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 xml 文件似乎以 UTF-8 字节顺序标记开头(请参阅此处< /a>)。可能是在您复制声明时发生的。解决方案取决于您使用的编辑器,其中一些可以设置为不写入 BOM。有时,当您删除文件的第一个字符并再次输入时,它会消失。
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.
我刚刚遇到了同样的问题,它实际上是由 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
我也遇到了同样的问题,但我发现这不是 XML 文档格式不正确的错误,有时会是这个问题,但这一次,这是由于没有使用服务器用来发送的正确输出流数据到客户端...
我的服务器端代码是这样的:
但是我的客户端代码是:
所以最后我更改了流以相互匹配,然后问题也解决了......
我仍然不明白为什么当这些流不同时会发生这种情况,即使这些流是由相同的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 :
but my client side code is :
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)