Xerces DOM解析器非常慢?

目前,我正在尝试使用JTidy清理HTML文件,将其转换为XHTML并将结果提供给DOM解析器。 以下代码是这些努力的结果:

public class HeaderBasedNewsProvider implements INewsProvider { /* ... */ public Collection getNewsEntries() throws NewsUnavailableException { Document document; try { document = getCleanedDocument(); } catch (Exception e) { throw new NewsUnavailableException(e); } System.err.println(document.getDocumentElement().getTextContent()); return null; } private final Document getCleanedDocument() throws IOException, SAXException, ParserConfigurationException { InputStream input = inputStreamProvider.getInputStream(); Tidy tidy = new Tidy(); tidy.setXHTML(true); ByteArrayOutputStream tidyOutputStream = new ByteArrayOutputStream(); tidy.parse(input, tidyOutputStream); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); InputStream domInputStream = new ByteArrayInputStream(tidyOutputStream.toByteArray()); System.err.println(factory.getClass()); return factory.newDocumentBuilder().parse(domInputStream); } } 

但是,我的系统上的DOM解析器实现(com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl)似乎非常慢。 即使对于如下所示的单行文档,解析也需要2-3分钟:

 

Nachricht vom 16. Juni 2011

Titel

Mitteilung weiter mehr Mitteilung

请注意 – 与DOM解析器相反 – JTidy在一秒钟内完成其工作。 因此,我怀疑我在某种程度上滥用了DOM API。

提前感谢您对此提出的任何建议!

即使没有validation,XML解析器也需要获取DTD,例如支持命名字符实体。 您应该考虑实现一个EntityResolver ,它将DTD请求解析为本地副本。

HTML dtd非常庞大,使用包含。 他们永远。 使用XML目录 。 可以在本地存储dtd并按系统ID映射它们。

如果你使用像maven这样的工具,你会找到足够的指针。

拦截实体作为公认答案的优点表明,您收到了正确的字符。