Android中解析xml文件的接口

写了Android中解析xml文件的接口。注意传入的xml路径类型是 
String path = "file:///data/data/com.example.androiddemo/network.xml";
否则会报异常的。
这个在Android模拟器上测试过,可以正常使用。


package com.example.androiddemo;import java.util.HashMap;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.Result;import javax.xml.transform.Source;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import android.util.Log;public class parseXML {private static final String TAG = "parseXMl";private static DocumentBuilderFactory factory = null;private static DocumentBuilder builder = null;private static Document doc = null;public static Document getDocument(String xml){if(doc != null) return doc;try {factory = DocumentBuilderFactory.newInstance();builder = factory.newDocumentBuilder();doc = builder.parse(xml);}catch(Exception ex){ex.printStackTrace();}return doc;}public static HashMap loadXML(String xml){doc = getDocument(xml);HashMap map = new HashMap();try {Element root = doc.getDocumentElement(); //获得root元素NodeList nl = root.getElementsByTagName("item"); //获得item标签的结点集合for(int i=0; i

你可能感兴趣的:(Android,java,编程)