Android,org.w3c.dom:没有可用的validationDocumentBuilder实现

我正在尝试解析Android 2.3.3上的XML文档,但似乎没有validation解析器。 我需要validation的原因是忽略XML文件中的空格(空格,回车,换行等)。

多数民众赞成我要如何解析文件:

DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); dbfac.setValidating(true); dbfac.setIgnoringElementContentWhitespace(true); DocumentBuilder docBuilder; docBuilder = dbfac.newDocumentBuilder(); Document d = docBuilder.parse(file); 

file是文件位置的URL作为字符串。 执行此代码的最后一行时,抛出以下exception:

 javax.xml.parsers.ParserConfigurationException: No validating DocumentBuilder implementation available 

当我取出dbfac.setValidating(true) ,没有exception发生,但后来我dbfac.setValidating(true)了空白问题。

有谁知道如何解决这个问题? 我必须使用另一个解析器吗?

在Android上,实现被硬编码为在validation集设置为true时抛出exception。 这是Android源代码链接 :

 @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; }