Android 开发中使用SAX解析xml小结

1. 解析类:

      继承和重写DefaultHandler 。

 

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

 

public class EmployeeHandler extends DefaultHandler {

    private static final String TAG = "xml";
    private List<Employee> data;
    private Employee employee;
    private String preTAG;
    
    private static final String _Employee="Employee";
    private static final String _name="name";
    private static final String _time="SignInTime";
    private static final String _phoneNumber = "phoneNumber";
    
    public EmployeeHandler(List<Employee> data){
        super();
        this.data = data;
    }
    //解析文档头
    @Override
    public void startDocument() throws SAXException {
//新建一个数组链表,元素为对象,用来存放xml解析到得数据
        if(data==null)
            data=new ArrayList<Employee>();
        Log.d(TAG, "startDocument ------- :");
        super.startDocument();
    }
    @Override
    public void endDocument() throws SAXException {
        Log.d(TAG, "endDocument ------- :");
        super.endDocument();
    }
    //文档结束标签
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        Log.d(TAG, "startElement ------- :localName :"+localName);
         preTAG = localName;
        if(_Employee.equalsIgnoreCase(localName)){
//新建用来存储标签之间的值的对象
            employee=new Employee();
        } 
        
        super.startElement(uri, localName, qName, attributes);
    }
    
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        preTAG = "";
        if(_Employee.equalsIgnoreCase(localName)){
            data.add(employee);
            Log.d(TAG, "endElement ------- :һ ");
        }
        super.endElement(uri, localName, qName);
    }
    //主要解析放在这里
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        String info = new String(ch,start,length);  
        Log.d(TAG, "characters ------- : property value :  " +info);
//将标签中之间的值存入对象字段
        if(preTAG.equalsIgnoreCase(_name)){
            employee.setName(info);
        } else if (preTAG.equalsIgnoreCase(_phoneNumber))
        {
        	employee.setPhoneNumber(info);
        }else if(preTAG.equalsIgnoreCase(_time)){
        	employee.setTime(info);
        }
        super.characters(ch, start, length);
    }
    
    public List<Employee> getdata(){
        return data;
    }
}
 
2.EmployeeXmlHelper类:
   封装了解析所需的前置、后置处理逻辑成工具类,方便调用。

 
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

 
public class EmployeeXmlHelper {
	public static List<Employee> parserXML(String str){
		if(null == str){
			return null;
		}
        String _str = str.trim();
        List<Employee> data = new ArrayList<Employee>();
        SAXParserFactory factory=SAXParserFactory.newInstance();
        XMLReader reader;
        try {
        	
        	byte[] bytes = _str.getBytes();
        	ByteArrayInputStream  bis = new ByteArrayInputStream (bytes);
            InputSource is = new InputSource(  (InputStream)bis);
            is.setEncoding("utf-8");
            reader = factory.newSAXParser().getXMLReader();
            reader.setContentHandler(new EmployeeHandler(data));  
            reader.parse(is);  
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return data;
    }
}

3.model类


package com.swift.phoneAttendance.model;


import java.io.Serializable;


public class Employee implements Serializable {
	String name = null;
	String phoneNumber = null;
	String time = null;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
} 
4.客户端调用方式:
   xml内容如下:
   <<Result>
	<Status>True<Status>
	<Employee>
       <Name></Name>
       <PhoneNumber></PhoneNumber>
     </ Employee>
	<Employee>
</ Result>
   调用解析:
   private List<Employee> data = EmployeeXmlHelper.parserXML(xml);


上一篇:orm2 中文文档 4.3 extendsTo(一对一关系)


下一篇:深入分析 Linux 内核链表