android xml解析demo,Android解析自定义xml文件--Dom解析xml文件,测试demo(方案四)...

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值