Java の文字列の java.Util.List のように表される多くの html ページ (つまり、そのソース コード) があります。Java の Document オブジェクトに変換する必要があります (パッケージ org.w3c.dom から)。
DocumentBuilderFactory と Document を使用して、次のようにします。
public static org.w3c.dom.Document inputStream2Document(InputStream inputStream) throws IOException, SAXException, ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
org.w3c.dom.Document parse = dbf.newDocumentBuilder().parse(inputStream);
return parse;
}
一部のページは正しい方法で変換されますが、他のページには間違った属性が記述されているなどの問題があり、有効ではありません (="" のない属性 ... のように見えます)。
<a href="somepage.html" someattr>
「someattr」と呼ばれる誤って書かれた属性の場合)。この場合、たとえば例外が発生します
Nested exception: org.xml.sax.SAXParseException; lineNumber: 7558; columnNumber: 71; Element type "a" must be followed by either attribute specifications, ">" or "/>".
また
Nested exception: org.xml.sax.SAXParseException; lineNumber: 109; columnNumber: 32; The string "--" is not permitted within comments.
この例外を無視するように DocumentBuilderFactory に伝える方法はありますか? これらのページもドキュメントに変換したいのですが、それらが無効であることは気にしません。