您的位置:首页 > 其它

安卓使用sax、dom、pull解析xml

2013-01-15 08:38 453 查看
XML:

<country>

<province id="1">

<name>hebei</name>

<city>baoding</city>

</province>

<province id="2">

<name>henan</name>

<city>kaifeng</city>

</province>

<province id="3">

<name>hunan</name>

<city>changsha</city>

</province>

</country>

解析:

JavaBean:

package com.csdn.hbsi.domain;

public class Country{

private Integer id;

private String name;

private String city;

public Country(String name,int age,String city){

super();

this.name=name;

this.age=age;

this.city=city;

}

public Country(){}

public String toString(){

return "person[id:"+getId()+"name:"+getName()+" city:"+getcity()+"]";

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getcity() {

return city;

}

public void setAge(String city) {

this.city = city;

}

}

service:

package com.csdn.hbsi.service;

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 com.csdn.hbsi.domain.Country;

public class PersonHandler extends DefaultHandler{

List<Country> Country;

Country country;

String elementTag = null;

public List<Country> getCountry(){

return Country;

}

@Override

public void characters(char[] ch, int start, int length)

throws SAXException {

if(elementTag!=null){

String data = new String(ch,start,length);

if("name".equals(elementTag)){

person.setName(data);

}else if("city".equals(elementTag)){

country.setcity(data);

}

}

}

@Override

public void endDocument() throws SAXException {

// TODO Auto-generated method stub

super.endDocument();

}

@Override

public void endElement(String uri, String localName, String qName)

throws SAXException {

if("country".equals(localName)&&person!=null){

country.add(city);

city = null;

}

elementTag = null;

}

@Override

public void startDocument() throws SAXException {//完成初始化的工作

country = new ArrayList<Country>();//初始化country

}

@Override

public void startElement(String uri, String localName, String qName,

Attributes attributes) throws SAXException {

if("country".equals(localName)){

country = new Country();

country.setId(new Integer(attributes.getValue(0)));

}

elementTag = localName;

}

}

SAX解析:

package com.csdn.hbsi.xml_parse;

import java.io.InputStream;

import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import com.csdn.hbsi.domain.Country;

import com.csdn.hbsi.service.CountryHandler;

public class SaxCountryService {

public List<Country> getCountryList(InputStream in) throws Exception, SAXException{

SAXParserFactory factory = SAXParserFactory.newInstance();

SAXParser parser = factory.newSAXParser();

CountryHandler dh = new CountryHandler();

parser.parse(in, dh);

List<Country> countryList = dh.getCountry();

in.close();

return countryList;

}

}

Dom解析:

package com.csdn.hbsi.xml_parse;//不需要Handler

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

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.Node;

import org.w3c.dom.NodeList;

import com.csdn.hbsi.domain.Country;

public class DomCountryService {

private static final List<Country> countryList = null;

public List<Country> getCountryList(InputStream in) throws Exception{

List<Country> country = new ArrayList<Country>();

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.parse(in);

Element root = document.getDocumentElement();//获取根节点

NodeList nodes = root.getElementsByTagName("country");

for(int i=0;i<nodes.getLength();i++){

Element countryelement = (Element) nodes.item(i);//拿到了第一个person元素

Country country = new Country();

country.setId(Integer.valueOf(countryelement.getAttribute("id")));

NodeList childNodes = countrylement.getChildNodes();

for(int j=0;j<childNodes.getLength();i++){

Node childeNode = childNodes.item(j);

if(childeNode.getNodeType()==Node.ELEMENT_NODE){

if("name".equals(childeNode.getNodeName())){//判断name节点

country.setName(childeNode.getFirstChild().getNodeValue());//获取那么节点中的值并放入Country中

}else if("age".equals(childeNode.getNodeName())){

country.setCity(childeNode.getFirstChild().getNodeValue());

}

}

}

countryList.add(country);

}

return countryList;

}

}

Pull解析:

package com.csdn.hbsi.xml_parse;

import java.io.IOException;

import java.io.InputStream;

import java.io.Writer;

import java.util.ArrayList;

import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;

import com.csdn.hbsi.domain.Country;

public class PullCountryService {

public List<Country> getCountryList(InputStream in) throws Exception {

List<Country> countryList = null;

XmlPullParser parser = Xml.newPullParser();// 获取解析器

parser.setInput(in, "UTF-8");// 获取输入流并设置编码方式

int eventType = parser.getEventType();

Country country = null;

while (eventType != XmlPullParser.END_DOCUMENT) {// 判断是否到结尾

switch (eventType) {

case XmlPullParser.START_DOCUMENT:

countryList = new ArrayList<Country>();

break;

case XmlPullParser.START_TAG:// 遇到节点

if ("country".equals(parser.getName())) {

country = new Country();

country.setId(Integer.valueOf(parser.getAttributeValue(0)));

}else if(country!=null){

if("name".equals(parser.getName())){

country.setName(parser.nextText());

}else if("age".equals(parser.getName())){

country.setCity(parser.nextText());

}

}

break;

case XmlPullParser.END_TAG:

if("country".equals(parser.getName())){

personList.add(country);

country = null;

}

break;

}

eventType = parser.next();

}

return countryList;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: