本文介绍了如何使用xmlStreamReader以字符串形式获取XML文件的全部内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在应用程序的一部分中,可以访问表示xmlStreamReader rel ="nofollow"> XML 文件需要完全读入 String
.
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
.
有没有一种方法可以获取XML内容,而无需构建另一个 xmlStreamReader
或在文件?
Is there a way to obtain the XML content without building another xmlStreamReader
or using another stream on the file?
推荐答案
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();
}
这篇关于如何使用xmlStreamReader以字符串形式获取XML文件的全部内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!