您的位置:首页 > 移动开发 > Android开发

android_xml解析 dom方法

2010-04-06 14:23 549 查看
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
void ReadXML()
{
DocumentBuilderFactory docBuilderFactory = null;
DocumentBuilder docBuilder = null;
Document doc = null;
try {
docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilder = docBuilderFactory.newDocumentBuilder();
//xml file 放到 assets目录中的
doc = docBuilder.parse(getResources().getAssets().open(”weather.xml”));
//root element
Element root = doc.getDocumentElement();
//Do something here
//get a NodeList by tagname
NodeList nodeList = root.getElementsByTagName(”tag”);
for(int i =0;i< nodeList.getLength();i++)
{
Node nd = nodeList.item(i);
//Read Node
}
} catch (IOException e) {
} catch (SAXException e) {
} catch (ParserConfigurationException e) {
} finally {
doc = null;
docBuilder = null;
docBuilderFactory = null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: