2

通过 Java/SAX 研究 XML 读/写,我发现只处理磁盘文件示例 - 我需要 STRING 输入/输出处理。我正在编写一个带有 XML 输入/输出的 WebService,所以我需要处理 STRING XML,而不是文件。

谢谢你。

4

1 回答 1

0

此示例适用于 Xerces,它是最著名的 XML Java 解析器之一:请参阅http://xerces.apache.org/xerces2-j/javadocs/api/org/xml/sax/XMLReader.html

parse

void parse(InputSource input)
           throws java.io.IOException,
                  SAXException

    Parse an XML document.

    The application can use this method to instruct the XML reader 
to begin parsing an XML document from any valid input source
 (a character stream, a byte stream, or a URI).

您将必须创建一个InputSource可以来自包括字符(字符串)在内的多种来源的。

http://xerces.apache.org/xerces2-j/javadocs/api/org/xml/sax/InputSource.html

public InputSource(java.io.InputStream byteStream)

    Create a new input source with a byte stream.





    Application writers should use setSystemId() to provide a base for 
resolving #relative URIs, may use setPublicId to include a public 
identifier, and may use setEncoding to specify the object's character encoding.

您可能会发现您的 XML 工具包有替代品,包括InputStream. 如果是这样,您可以使用ByteArrayInputStream从您的字符串形成的。有点啰嗦,但它确实有效。

于 2013-05-07T20:28:03.723 回答