TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
创建一个java类MyContentHandler.java继承DefaultHandler.java(ContentHandler.java的实现类)读取XML中的数据:-
- package com.zhangjie.xml;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- public class MyContentHandler extends DefaultHandler {
- String hisname,address,money,sex,status;
- String tagName;
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- if(tagName.equals("name"))
- hisname = new String(ch,start,length);
- else if(tagName.equals("sex"))
- sex = new String(ch,start,length);
- else if(tagName.equals("status"))
- status = new String(ch,start,length);
- else if(tagName.equals("address"))
- address = new String(ch,start,length);
- else if (tagName.equals("money"))
- money = new String(ch,start,length);
- }
- @Override
- public void endDocument() throws SAXException {
- System.out.println("-------------end----------");
- }
- @Override
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- //在worker标签解析完之后,会打印出所有得到的数据
- tagName = "";
- if(localName.equals("worker")){
- this.printout();
- }
- }
- @Override
- public void startDocument() throws SAXException {
- System.out.println("-----------begin----------");
- }
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attr) throws SAXException {
- tagName = localName;
- if(localName.equals("worker")){
- //获取标签的全部属性
- for(int i = 0 ;i < attr.getLength();i++){
- System.out.println(attr.getLocalName(i)+"="+attr.getValue(i));
- }
- }
- }
- private void printout(){
- System.out.print("name:");
- System.out.println(hisname);
- System.out.print("sex:");
- System.out.println(sex);
- System.out.print("status:");
- System.out.println(status);
- System.out.print("address:");
- System.out.println(address);
- System.out.print("money:");
- System.out.println(money);
- System.out.println();
- }
- }
复制代码
在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"
- >
- <Button
- android:id="@+id/jiexi"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="解析xml文件"
- />
- </LinearLayout>
复制代码
并在XMLActivity.java中设置其监听器,此例为先从网络上下载xml文件,再读取,打印到LogCat中:-
- package com.zhangjie.xml;
- import java.io.StringReader;
- import javax.xml.parsers.SAXParserFactory;
- import org.xml.sax.InputSource;
- import org.xml.sax.XMLReader;
- import com.zhangjie.utils.HttpDownloader;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class XmlActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button button = (Button)findViewById(R.id.jiexi);
- button.setOnClickListener(new XMLButtonListener());
- }
-
- class XMLButtonListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- HttpDownloader hd = new HttpDownloader();
- String resultStr = hd.download("http://www.javaxxz.com/new.xml");
- System.out.println(resultStr);
- try{
- //创建一个SAXParserFactory
- SAXParserFactory factory = SAXParserFactory.newInstance();
- XMLReader reader = factory.newSAXParser().getXMLReader();
- //为XMLReader设置内容处理器
- reader.setContentHandler(new MyContentHandler());
- reader.parse(new InputSource(new StringReader(resultStr)));
- }catch(Exception e){
- e.printStackTrace();
- }
-
- }
- }
- }
复制代码
注:有关如何下载文本文件的例子,请参考帖子:http://www.javaxxz.com/read.php?tid=1212
http://www.javaxxz.com/new.xml 网络上的xml文件如下:
当单击“解析xml文件”按钮后在LoGCat中打印的信息:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|