相关代码;实例化SAXSource的barfs:

TransformerFactory factory = TransformerFactory.newInstance();
XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser");
Source input = new SAXSource(xmlReader, "http://books.toscrape.com/");
Result output = new StreamResult(System.out);
factory.newTransformer().transform(input, output);


JavaDoc的say

public SAXSource(XMLReader reader,
         InputSource inputSource)



  使用XMLReader和SAX InputSource创建一个SAXSource。的
  Transformer或SAXTransformerFactory会将自己设置为
  阅读器的ContentHandler,然后将调用reader.parse(inputSource)。


查看InputSource显示:

InputSource(InputStream byteStream)
Create a new input source with a byte stream.
InputSource(Reader characterStream)
Create a new input source with a character stream.


因此,对于example,这需要在html中读取InputStream的字符流?

tagsoup更好地用于this身份转换吗?但是,如何?

最佳答案

有一个采用系统ID的构造函数https://docs.oracle.com/javase/8/docs/api/org/xml/sax/InputSource.html#InputSource-java.lang.String-,例如URL,以便您可以使用Source input = new SAXSource(xmlReader, new InputSource("http://books.toscrape.com/"));

09-30 17:43