SAX error: incompatible types: String cannot be converted to InputSource

為{幸葍}努か 提交于 2019-12-08 09:35:17

问题


Relevant code; barfs on instantiating the SAXSource:

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);

The JavaDoc's say:

public SAXSource(XMLReader reader,
         InputSource inputSource)

Create a SAXSource, using an XMLReader and a SAX InputSource. The Transformer or SAXTransformerFactory will set itself to be the reader's ContentHandler, and then will call reader.parse(inputSource).

Looking at InputSource shows:

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

So this would entail, for example, a character stream to read in html for the InputStream??

Would tagsoup better be used for this identity transform? But, how?


回答1:


There is a constructor https://docs.oracle.com/javase/8/docs/api/org/xml/sax/InputSource.html#InputSource-java.lang.String- that takes a system id e.g. a URL so you can use Source input = new SAXSource(xmlReader, new InputSource("http://books.toscrape.com/"));.




回答2:


You can get access to an InputStream that reads from the resource behind the URL like this:

InputStream i = new URL("http://...").openConnection().getInputStream();

Then you can use i for your SAXSource.



来源:https://stackoverflow.com/questions/54041860/sax-error-incompatible-types-string-cannot-be-converted-to-inputsource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!