目前,我正在嘗試使用JTidy清理HTML文件,將其轉換爲XHTML並將結果提供給DOM解析器。下面的代碼是這些努力的結果:Xerces DOM解析器非常慢?
public class HeaderBasedNewsProvider implements INewsProvider {
/* ... */
public Collection<INewsEntry> 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分鐘:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title></title></head><body><div class="text"><h2>Nachricht vom 16. Juni 2011</h2><h1>Titel</h1><p>Mitteilung <a href="dokumente/medienmitteilungen/MM_NR_jglp.pdf" target="_blank">weiter</a> mehr Mitteilung</p></div></body></html>
注意 - 在對比的是DOM解析器 - JTidy完成一秒鐘內工作。因此,我懷疑我在某種程度上濫用了DOM API。
在此先感謝您的任何建議!
很奇怪。在這樣一個小文件上不應該接近那麼慢。而且,由於您明確將驗證設置爲false,因此我不希望它從DTD解析內容。你可以通過一個分析器來查看哪些電話花費最多時間? –