Dom解析xml,测试demo
DomParseXml.java (在activity的onCreate等方法中执行parseXml方法运行即可)
package com.example.androidtest;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import android.content.Context;
import android.util.Log;
public class DomParseXml {
public static final String LOG_TAG = "DomParseXml";
private static final String sTag1 = "test_item";
private static final String sTag2 = "test_folder";
private static ArrayList sAll = new ArrayList();
public static void parseXml(Context context){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
//获取InputStream的两种方式
//InputStream in = context.getResources().openRawResource(R.raw.test_config);
InputStream in = context.getAssets().open("test_config1.xml");
Document document = builder.parse(in);
Element root = document.getDocumentElement();
Log.d(LOG_TAG, "root name is:"+root.getNodeName());
NodeList childList = root.getChildNodes();
for(int i=0;i
StringBuffer temp = new StringBuffer();
Node element = childList.item(i);
//Element element = (Element) childList.item(i);
String tagName = element.getNodeName();
Log.d(LOG_TAG, "tag name is:"+tagName);
if(!sTag1.equals(tagName)&&!sTag2.equals(tagName)){
continue;
}
String text = element.getTextContent();
Log.d(LOG_TAG, "text is:"+text);
String text1 = element.getNodeValue();
Log.d(LOG_TAG, "text1 is:"+text1);
NamedNodeMap attributes = element.getAttributes();
//----------------------------------------------
temp.append(tagName);
//----------------------------------------------
for(int j=0;j
Node childNode = attributes.item(j);
String childName = childNode.getNodeName();
Log.d(LOG_TAG, "child name is:"+childName);
String childValue = childNode.getNodeValue();
Log.d(LOG_TAG, "child value is:"+childValue);
String childtext = childNode.getTextContent();
Log.d(LOG_TAG, "child text is:"+childtext);
//----------------------------------------------
temp.append("--");
temp.append(childName);
temp.append("--");
temp.append(childValue);
//----------------------------------------------
}
temp.append("--");
temp.append(text);
sAll.add(temp.toString());
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//-------打印
for(String s:sAll){
Log.d(LOG_TAG, "Dom parse is:"+s);
}
}
}
xml文件 test_config1.xml
你们
是
好
孩子
测试结果:
D/DomParseXml(11085): Dom parse is:test_item--item_name--item1--你们
D/DomParseXml(11085): Dom parse is:test_item--item_name--item2--是
D/DomParseXml(11085): Dom parse is:test_folder--folder_name--folder1--好
D/DomParseXml(11085): Dom parse is:test_folder--folder_name--folder2--孩子