【发布时间】:2012-11-03 06:47:36
【问题描述】:
我正在使用 Dom4j 解析 HTML 文档。 Dom4j 需要 XML,因此不声明 HTML 实体。 可以在文档的 DTD 中声明它们,但我正在解析外部输入,所以这是不合适的。我宁愿在解析器中以编程方式声明它们。
这是我的代码:
// Read.
final DocumentFactory df = DOMDocumentFactory.getInstance();
SAXReader reader = new SAXReader();
Document doc, outDoc;
try {
doc = reader.read( new StringReader(htmlStr) );
}
catch( Exception ex ){
throw new RuntimeException("Error parsing the HTML:\n " + ex.toString() );
}
我看到 SAXReader 有 reader.setEntityResolver( ??? ); 但似乎不是解决方案,因为可覆盖的方法如下所示:
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
我在寻找什么类似于
reader.setTrueEntityResolver( new EntityResolver(){
public InputStream resolve( String name ){ ... }
}
【问题讨论】: