[英]How to get the full content of an XML file as a String using XmlStreamReader?
I'm in a part of an application where I have an access to the XmlStreamReader
which is representing the XML file needed to be fully read into a String
.我在应用程序的一部分中,我可以访问XmlStreamReader
,它表示需要完全读入String
的XML文件。
Is there a way to obtain the XML content without building another XmlStreamReader
or using another stream on the file ?有没有办法在不构建另一个XmlStreamReader
或在文件上使用另一个流的情况下获取 XML 内容?
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
private String getOuterXml(XMLStreamReader xmlr) throws TransformerConfigurationException,
TransformerFactoryConfigurationError, TransformerException
{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringWriter stringWriter = new StringWriter();
transformer.transform(new StAXSource(xmlr), new StreamResult(stringWriter));
return stringWriter.toString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.