Android,org.w3c.dom:没有验证的DocumentBuilder实现可用 [英] Android, org.w3c.dom: No validating DocumentBuilder implementation available
问题描述
我试图在Android 2.3.3上解析XML文档,但似乎没有验证的解析器。我需要验证的原因是忽略XML文件中的空格(空格,回车符,换行符等)。
I'm trying to parse an XML document on Android 2.3.3, but it seems that there is no validating parser. The reason I need validation for is to ignore whitespaces in the XML file (blanks, carriage returns, line feeds, etc.).
这是我要解析文档:
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
dbfac.setValidating(true);
dbfac.setIgnoringElementContentWhitespace(true);
DocumentBuilder docBuilder;
docBuilder = dbfac.newDocumentBuilder();
Document d = docBuilder.parse(file);
文件
是文件位置的URL作为字符串。当执行此代码的最后一行时,会抛出以下异常:
file
is the URL to the file location as string. When executing the last line of this code the following exception is thrown:
javax.xml.parsers.ParserConfigurationException: No validating DocumentBuilder implementation available
当我取出 dbfac.setValidating(true)
,没有异常,但是我有空白的问题。
When I take out dbfac.setValidating(true)
, no exception occurs, but then I have the problem with the whitespaces.
有谁知道如何解决这个问题?我必须使用另一个解析器吗?
Does anyone know how to solve this problem? Do I have to use another parser?
推荐答案
在Android上,实现是硬编码的,当验证集设置为真正。这里是 Android源代码链接:
On Android, the implementation is hard coded to throw the exception when validation set set to true. Here is the Android source code link:
@Override
public DocumentBuilder newDocumentBuilder()
throws ParserConfigurationException {
if (isValidating()) {
throw new ParserConfigurationException(
"No validating DocumentBuilder implementation available");
}
/**
* TODO If Android is going to support a different DocumentBuilder
* implementations, this should be wired here. If we wanted to
* allow different implementations, these could be distinguished by
* a special feature (like http://www.org.apache.harmony.com/xml/expat)
* or by throwing the full SPI monty at it.
*/
DocumentBuilderImpl builder = new DocumentBuilderImpl();
builder.setCoalescing(isCoalescing());
builder.setIgnoreComments(isIgnoringComments());
builder.setIgnoreElementContentWhitespace(isIgnoringElementContentWhitespace());
builder.setNamespaceAware(isNamespaceAware());
// TODO What about expandEntityReferences?
return builder;
}
这篇关于Android,org.w3c.dom:没有验证的DocumentBuilder实现可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!