XML字符串解析在Android中? [英] XML String parsing in Android?
本文介绍了XML字符串解析在Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好所有我用下面的code解析XML文件。
的DocumentBuilderFactory厂;
DocumentBuilder的建设者;
InputStream的是;
DOM文档;
尝试 {
工厂= DocumentBuilderFactory.newInstance();
是=新的FileInputStream(strFileName);
建设者= factory.newDocumentBuilder();
DOM = builder.parse(是);
}
赶上(例外五){}
而不是XML文件中有没有办法来解析字符串。
XML字符串=< XML版本=1.0><名称>应用< /名称><演示>演示< /演示>< /的xml>中;
解决方案
您可以将您的字符串转换为使用InputStream <一href="http://developer.android.com/reference/java/io/ByteArrayInputStream.html">ByteArrayInputStream:
XML字符串=有效的XML在这里;
InputStream的是=新ByteArrayInputStream的(xml.getBytes(UTF-8));
DOM = builder.parse(是);
Hello to all 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 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);
这篇关于XML字符串解析在Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文