DOM方式解析xml文件2(解析本地xml)

这个与1的方法唯一不同的就是传递InputSource的参数:
上代码:
1.DomParseXml.java
package cn.com;

import java.io.File;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class DomParseXml extends Activity implements OnClickListener {

	File fl;
	ListView lv;

	ArrayAdapter<String> adapter;

	private EditText et;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		Button bt = (Button) findViewById(R.id.bt);

		bt.setOnClickListener(this);

		et = (EditText) findViewById(R.id.et);

	}

	@Override
	public void onClick(View agr0) {

		String[] data = null;
		Node nRoot = root("root").item(0);
		// 获得孩子结点
		NodeList child = nRoot.getChildNodes();
		// 获得子节点的个数 
		int childSize = child.getLength();
		System.out.println("**********childSize************" + childSize);

		data = new String[childSize];

		for (int i = 0; i < childSize; i++) {
			// 第一个子节点,city
			Node nChild = child.item(i);
			// 获得子节点
			if (nChild.hasAttributes()) {
				// 获得节点属性
				data[i] = nChild.getAttributes().getNamedItem("id")
						.getNodeValue();
				System.out.println("**********************" + data[i]);

			}
		}
		setTitle(et.getText().toString()); // 显示查询的城市
	}

	private NodeList root(final String str) {

		NodeList root = null;

		try {

			String xml = "<root><demo id='73'><name>MjAxMMrAveexrQ==</name><type>0+nA1g==</type><demoTitle>MjAxMMrAveexrQ==</demoTitle><size></size><version>djEuMA==</version><releaseDate>MjAxMC0wNi0yNSAxNjozMTozOC4w</releaseDate><language></language><releaseFunction>uabE3Mu1w/c=</releaseFunction><releaseDetail></releaseDetail><picRoot><pic id='133' type='jpg' order='1'></pic><pic id='134' type='jpg' order='1'></pic></picRoot><videoRoot></videoRoot></demo><demo id='74'><name>y66yqLutwMg=</name><type>0+nA1g==</type><demoTitle>y66yqLutwMg=</demoTitle><size></size><version>djEuMA==</version><releaseDate>MjAxMC0wNi0yNSAxNjozMToyMy4w</releaseDate><language></language><releaseFunction>uabE3Mu1w/c=</releaseFunction><releaseDetail></releaseDetail><picRoot><pic id='135' type='jpg' order='1'></pic><pic id='136' type='jpg' order='1'></pic></picRoot><videoRoot></videoRoot></demo><demo id='75'><name>t++7y8340MLOxQ==</name><type>0+nA1g==</type><demoTitle>t++7y8340MLOxQ==</demoTitle><size></size><version>djEuMA==</version><releaseDate>MjAxMC0wNi0yNSAxNjozNzowMC4w</releaseDate><language></language><releaseFunction>uabE3Mu1w/c=</releaseFunction><releaseDetail></releaseDetail><picRoot><pic id='137' type='jpg' order='1'></pic><pic id='138' type='jpg' order='1'></pic></picRoot><videoRoot></videoRoot></demo><demo id='76'><name>svrGt8P7s8Y=</name><type>0+nA1g==</type><demoTitle>svrGt7HqzOI=</demoTitle><size>Mjk4MzU3Nw==</size><version>djEuMA==</version><releaseDate>MjAxMC0wNi0zMCAxNTo1MTo0MS4w</releaseDate><language></language><releaseFunction>uabE3Mu1w/c=</releaseFunction><releaseDetail>svrGt8/qz7jLtcP3</releaseDetail><picRoot><pic id='139' type='jpg' order='1'></pic><pic id='140' type='jpg' order='1'></pic></picRoot><videoRoot><video id='68' type='flv' order='0'>0d3KvrDmw/uzxg==</video></videoRoot></demo><demo id='77'><name>svrGt8P7s8Y=</name><type>0+nA1g==</type><demoTitle>svrGt7HqzOI=</demoTitle><size></size><version>djEuMA==</version><releaseDate>MjAxMC0wNy0wMSAxMzo0NTo0MS4w</releaseDate><language></language><releaseFunction>uabE3Mu1w/c=</releaseFunction><releaseDetail></releaseDetail><picRoot><pic id='141' type='png' order='1'></pic><pic id='142' type='png' order='1'></pic></picRoot><videoRoot><video id='69' type='flv' order='0'>0d3KvrDmw/uzxg==</video></videoRoot></demo></root>";

			// 创建一个新的字符串
			StringReader read = new StringReader(xml);

			InputSource is = new InputSource(read);

			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder dom = factory.newDocumentBuilder();

			Document doc = dom.parse(is);

			root = doc.getElementsByTagName(str);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return root;

	}

}


2.main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
	<EditText android:text="" android:id="@+id/et"
		android:layout_width="150dp" android:layout_height="wrap_content"></EditText>
	<Button android:text="button" android:id="@+id/bt"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>


你可能感兴趣的:(C++,c,xml,android,C#)