转载请注明出处:http://blog.csdn.net/droyon/article/details/9347161

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<String> sAll = new ArrayList<String>();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<childList.getLength();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<attributes.getLength();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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SAXException e) {// TODO Auto-generated catch blocke.printStackTrace();}//-------打印for(String s:sAll){Log.d(LOG_TAG, "Dom parse is:"+s);}}}

xml文件 test_config1.xml

<?xml version="1.0" encoding="utf-8"?><test_config version="1">    <test_item item_name="item1">你们</test_item>    <test_item item_name="item2">是</test_item>    <test_folder folder_name="folder1">好</test_folder>    <test_folder folder_name="folder2">孩子</test_folder></test_config>

测试结果:

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--孩子

更多相关文章

  1. Android(安卓)硬件加速
  2. Android(安卓)检测SD卡是否可用
  3. Android(安卓)Tips1
  4. android拖拽功能的实现原理
  5. Android之Canvas
  6. Android之关于onSaveInstanceState和onRestoreInstanceState触发
  7. 移动数据流程
  8. android 生成快捷方式(包括源码)
  9. Android实现ListView数据动态加载的方法

随机推荐

  1. 2016-9-13
  2. Android(安卓)小游戏2048 代码简单实现
  3. Activity跳转短暂黑屏
  4. Android(安卓)判断程序处于前台或者后台
  5. 定义界面布局TextView&Button
  6. Android判断当前应用通知是否打开
  7. android配置activity
  8. React Native跳转android原生界面
  9. ListView中一些效果的实现
  10. android 实现ListView中添加RaidoButton