SAX 错误:类型不兼容:字符串无法转换为 InputSource [英] SAX error: incompatible types: String cannot be converted to InputSource

查看:73
本文介绍了SAX 错误:类型不兼容:字符串无法转换为 InputSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关代码;关于实例化 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)

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?

解决方案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆