问题描述
相关代码;关于实例化 SAXSource
的 barfs:
TransformerFactory factory = TransformerFactory.newInstance();XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser");源输入 = new SAXSource(xmlReader, "http://books.toscrape.com/");结果输出 = new StreamResult(System.out);factory.newTransformer().transform(input, output);
JavaDoc 的 说:
public SAXSource(XMLReader reader,输入源输入源)
使用 XMLReader 和 SAX InputSource 创建 SAXSource.这Transformer 或 SAXTransformerFactory 将自己设置为reader 的 ContentHandler,然后会调用 reader.parse(inputSource).
查看 InputSource
显示:
InputSource(InputStream byteStream)使用字节流创建一个新的输入源.输入源(读者字符流)使用字符流创建一个新的输入源.
因此,对于示例,这将需要一个为 InputStream
??
html
中读取的字符流tagsoup
是否会更好地用于此身份转换?但是,如何?
有一个构造函数 https://docs.oracle.com/javase/8/docs/api/org/xml/sax/InputSource.html#InputSource-java.lang.String- 需要一个系统 ID 例如一个 URL,以便您可以使用 Source input = new SAXSource(xmlReader, new InputSource("http://books.toscrape.com/"));
.
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)
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?
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/"));
.
这篇关于SAX 错误:类型不兼容:字符串无法转换为 InputSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!