XML String parsing in Android?
I use the following code to parse the XML file.
DocumentBuilderFactory factory;
DocumentBuilder builder;
InputStream is;
Document dom;
try {
factory = DocumentBuilderFactory.newInstance();
is = new FileInputStream(strFileName);
builder = factory.newDocumentBuilder();
dom = builder.parse(is);
}
catch(Exception e){}
Instead of XML file is there any开发者_如何学JAVA way to parse the String.
String xml="<?xml version="1.0"?> <name> Application</name> <demo> Demo </demo> </xml>";
You can convert your string to an InputStream using ByteArrayInputStream:
String xml ="valid xml here";
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
dom = builder.parse(is);
You can use StringReader :
StringReader sr = new StringReader(xml);
InputSource is = new InputSource(sr);
Document d = builder.parse(is);
精彩评论