安卓使用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;
}
}
<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;
}
}
相关文章推荐
- 安卓使用DOM,SAX,PULL解析xml,安卓写入xml文件
- Android学习笔记_7_使用 sax 或者 dom 或者 pull 解析XML文件
- android中使用SAX, DOM, 或者pull 来解析XML文档
- Android 使用SAX或者DOM或者pull解析XML文件
- 安卓笔记之XML解析(DOM、SAX、PULL)
- android学习——使用SAX、DOM 和 PULL 解析xml文件,及使用pull生成xml文件
- Java乔晓松-使用SAX,DOM,pull解析XML文件以及pull解析生成xml文件
- 使用SAX或者DOM或者pull解析XML文件
- Android 使用SAX或者DOM或者pull解析XML文件
- Android 使用Dom与SAX解析xml文档的方式
- Android中使用DOM解析xml时出现org.xml.sax.SAXParseException: Unexpected token
- Java解析xml的主要解析器: SAX和DOM的选择(附上新方法--Pull解析)
- Dom sax pull解析xml的差异
- android解析XML总结(SAX、Pull、Dom三种方式) <转载>
- Android系列--DOM、SAX、Pull解析XML
- Android-解析XML(Dom,Sax,Pull)
- 解析XML——DOM、Pull、SAX
- android解析XML总结(SAX、Pull、Dom三种方式)
- Android平台中实现对XML的三种解析方式(DOM 、PULL、 SAX)
- (五十七)android解析XML总结(SAX、Pull、Dom三种方式)