[英]How to get the full content of an XML file as a String using XmlStreamReader?
[英]Convert the string content into an XMLStreamReader
您好我想知道我们如何转换XML标签形式的字符串内容,我需要将其转换为XMLStreamReader
您可以使用XMLInputFactory.createXMLStreamReader
,传入StringReader
来包装您的字符串。
String text = "<foo>This is some XML</foo>";
Reader reader = new StringReader(text);
XMLInputFactory factory = XMLInputFactory.newInstance(); // Or newFactory()
XMLStreamReader xmlReader = factory.createXMLStreamReader(reader);
我假设你想通过XMLStreamReader
从String
读取XML内容。 你可以这样做:
public XMLStreamReader readXMLFromString(final String xmlContent)
{
final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
final StringReader reader = new StringReader(xmlContent);
return inputFactory.createXMLStreamReader(reader);
}
//Intialize XMLInputFactory
XMLInputFactory factory = XMLInputFactory.newInstance();
//Reading from xml file and creating XMLStreamReader
XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(
file));
String currentElement = "";
//Reading all the data
while(reader.hasNext()) {
int next = reader.next();
if(next == XMLStreamReader.START_ELEMENT)
currentElement = reader.getLocalName();
//System.out.println(currentElement);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.