您好我想知道我们如何转换 XML 标签形式的字符串内容,我需要将其转换为 XMLStreamReader
问问题
19293 次
3 回答
38
您可以使用XMLInputFactory.createXMLStreamReader
, 传入 aStringReader
来包装您的字符串。
String text = "<foo>This is some XML</foo>";
Reader reader = new StringReader(text);
XMLInputFactory factory = XMLInputFactory.newInstance(); // Or newFactory()
XMLStreamReader xmlReader = factory.createXMLStreamReader(reader);
于 2013-09-11T05:46:43.007 回答
3
我假设您想String
通过XMLStreamReader
. 你可以这样做:
public XMLStreamReader readXMLFromString(final String xmlContent)
{
final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
final StringReader reader = new StringReader(xmlContent);
return inputFactory.createXMLStreamReader(reader);
}
于 2013-09-11T05:51:36.160 回答
0
//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);
}
于 2013-09-11T05:54:48.400 回答