XML SAX解析器 - 忽略绑定preFIX例外 [英] XML sax parser - ignore unbound prefix exception

查看:160
本文介绍了XML SAX解析器 - 忽略绑定preFIX例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当解析Android中的XML文件,我做的是这样的:

When parsing an xml file in android, I'm doing like this:

try
{
    InputStream is = ...
    MyContentHandler ch = new MyContentHandler();
    Xml.parse(is, Encoding.UTF_8, ch);
}
catch ...

问题是,有时我试图解析该文件没有很好地形成。
就我而言,不宣而命名空间可能是present。

The problem is that sometimes the file I'm trying to parse is not well-formed. In my case, undeclared namespaces may be present.

我感兴趣的数据是不是这些标签,所以我可以简单地忽略它里面,但我得到绑定preFIX的异常不是内容处理程序中,但在解析器;这意味着,如果发生异常的整个分析过程被中断。

The data I'm interested in is not inside those tags so I could simply ignore it, but I get an exception of unbound prefix not inside the content handler but in the parser itself; this means that if the exception occurs the entire parsing process is interrupted.

有没有使用SAX解析器忽略这种错误的方式(或命名空间全部)?

Is there a way of using the sax parser ignoring this kind of error (or namespaces at all)?

P.S。我想避免加载所有文件在内存中为一个字符串,命名空间带出,或不必重写文件。

p.s. I want to avoid loading all the file in memory as a string and strip namespaces out of it, or having to rewrite the file.

推荐答案

我发现在另一个线程解决方案。

I found the solution in another thread.

而不是使用Xml.parse您需要手动实例通过的SAXParserFactory SAX解析器,并得到读者。

Instead of using Xml.parse you need to manually instantiate a sax parser through the SAXParserFactory and get a reader.

您可以再设置读卡器功能。

You can then set the reader features.

在现有的特征,一是禁用命名空间,并且做的伎俩。

Among the available features, one disables namespaces and that does the trick.

参考 - > <一个href=\"http://stackoverflow.com/questions/5481043/how-can-i-configure-a-sax-parser-in-the-sun-jvm-to-match-the-android-one\">LINK

这篇关于XML SAX解析器 - 忽略绑定preFIX例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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