Android解析自定义xml文件--Dom解析xml文件,测试demo(方案四)
16lz
2021-01-25
转载请注明出处: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--孩子
更多相关文章
- Android(安卓)硬件加速
- Android(安卓)检测SD卡是否可用
- Android(安卓)Tips1
- android拖拽功能的实现原理
- Android之Canvas
- Android之关于onSaveInstanceState和onRestoreInstanceState触发
- 移动数据流程
- android 生成快捷方式(包括源码)
- Android实现ListView数据动态加载的方法