Java WebService:String XML SAX读/写,不是通过文件

时间:2013-05-07 20:18:51

标签: java xml string web-services sax

研究Java / SAX的XML读/写,我发现只处理磁盘文件的例子 - 我需要STRING输入/输出处理。我正在使用XML输入/输出编写WebService,因此我需要处理STRING XML,而不是文件。

谢谢。

1 个答案:

答案 0 :(得分: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。有点啰嗦,但它确实有效。