问题
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