利用pull解析器解析服务器返回的String类型的XML

前端之家收集整理的这篇文章主要介绍了利用pull解析器解析服务器返回的String类型的XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1,从服务器返回的字符串样式的xml格式:

detail(1824): <?xml version="1.0" encoding="UTF-8"?><info><resultflag>1</resultflag><list count="5"><company><id>1</id><cname>西宇信息咨询有限公司</cname><legal>张曼</legal><businesslicense>000000</businesslicense><linkphone>029-87218995</linkphone><fax>029-87218996</fax><address>西安市北大街2号金钟大厦B座719室</address><mcompanyid>1</mcompanyid><disorder>1</disorder><createtime></createtime><createid>admin</createid><updateid>admin</updateid>2013-03-11 14:21:24</company><company><id>2</id><cname>西宇网络广告</cname><legal>赵严</legal><businesslicense>000000</businesslicense><linkphone>029-87218995</linkphone><fax>029-87218996</fax><address>西安市北大街2号金钟大厦B座719室</address><mcompanyid>1</mcompanyid><disorder>1</disorder><createtime></createtime><createid>admin</createid><updateid>admin</updateid>2013-03-11 14:28:21</company><company><id>3</id><cname>西宇金融担保</cname><legal>王建丽</legal><businesslicense>000000</businesslicense><linkphone>029-87218995</linkphone><fax>029-87218996</fax><address>西安市北大街2号金钟大厦B座719室</address><mcompanyid>1</mcompanyid><disorder>2</disorder><createtime></createtime><createid>admin</createid><updateid>admin</updateid>2012-12-17 03:11:02</company><company><id>4</id><cname>西宇中介连锁</cname><legal>古宁</legal><businesslicense>000000</businesslicense><linkphone>029-87218995</linkphone><fax>029-87218996</fax><address>西安市北大街2号金钟大厦B座719室</address><mcompanyid>1</mcompanyid><disorder>3</disorder><createtime></createtime><createid>admin</createid><updateid>admin</updateid>2012-12-17 15:10:17</company><company><id>5</id><cname>西宇新房集团</cname><legal></legal><businesslicense></businesslicense><linkphone></linkphone><fax></fax><address></address><mcompanyid>1</mcompanyid><disorder>4</disorder><createtime>2013-03-11 14:22:03</createtime><createid>admin</createid><updateid>admin</updateid>2013-03-11 14:22:03</company></info>

注意:这是一长串的字符串,不是xml文本,也没有文件名,就当与一对字符串,现在要将此用pull解析

2,对应的实体类

package com.cn.test;

public class Company {
 private String id ;
 private String cnname;
 private String legal;
 private String businesslicense;
 private String linkphone;
 private String fax;
 private String address;
 private String mcompanyid;
 private String disorder;
 private String createtime;
 private String createid;
 private String updateid;
 private String updatetime;
public String getId() {
	return id;
}
public void setId(String id) {
	this.id = id;
}
public String getCnname() {
	return cnname;
}
public void setCnname(String cnname) {
	this.cnname = cnname;
}
public String getLegal() {
	return legal;
}
public void setLegal(String legal) {
	this.legal = legal;
}
public String getBusinesslicense() {
	return businesslicense;
}
public void setBusinesslicense(String businesslicense) {
	this.businesslicense = businesslicense;
}
public String getLinkphone() {
	return linkphone;
}
public void setLinkphone(String linkphone) {
	this.linkphone = linkphone;
}
public String getFax() {
	return fax;
}
public void setFax(String fax) {
	this.fax = fax;
}
public String getAddress() {
	return address;
}
public void setAddress(String address) {
	this.address = address;
}
public String getMcompanyid() {
	return mcompanyid;
}
public void setMcompanyid(String mcompanyid) {
	this.mcompanyid = mcompanyid;
}
public String getDisorder() {
	return disorder;
}
public void setDisorder(String disorder) {
	this.disorder = disorder;
}
public String getCreatetime() {
	return createtime;
}
public void setCreatetime(String createtime) {
	this.createtime = createtime;
}
public String getCreateid() {
	return createid;
}
public void setCreateid(String createid) {
	this.createid = createid;
}
public String getUpdateid() {
	return updateid;
}
public void setUpdateid(String updateid) {
	this.updateid = updateid;
}
public String getUpdatetime() {
	return updatetime;
}
public void setUpdatetime(String updatetime) {
	this.updatetime = updatetime;
}
 
 
}


3 解析工具类

package com.cn.test;

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

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class GetCompanyInfoService {
List<Company> companys;

private Objectdetail;//服务器返回来存放字符串xml对象
ByteArrayInputStream bis=new ByteArrayInputStream(detail.toString().getBytes());
public List<Company> PullParseXML(){

List<Company> list=null;
Company company = null;

//构建XmlPullParserFactory
try {
XmlPullParserFactory pullParserFactory=XmlPullParserFactory.newInstance();
//获取XmlPullParser的实例
XmlPullParser xmlPullParser=pullParserFactory.newPullParser();
//设置输入流 xml文件
xmlPullParser.setInput(ins,"UTF-8");

//开始
int eventType=xmlPullParser.getEventType();

try {
while(eventType!=XmlPullParser.END_DOCUMENT){
String nodeName=xmlPullParser.getName();
switch (eventType) {
//文档开始
case XmlPullParser.START_DOCUMENT:
list=new ArrayList<Company>();
break;
//开始节点
case XmlPullParser.START_TAG:
//判断如果其实节点为company
if("company".equals(nodeName)){
//实例化student对象
company=new Company();
}else if("id".equals(nodeName)){
//设置name
company.setId(xmlPullParser.nextText());
}else if("cname".equals(nodeName)){
//设置sex
company.setCnname(xmlPullParser.nextText());
}else if("legal".equals(nodeName)){
//设置age
company.setLegal(xmlPullParser.nextText());
}else if("businesslicense".equals(nodeName)){
//设置email
company.setBusinesslicense(xmlPullParser.nextText());
}else if("linkphone".equals(nodeName)){
//设置birthday
company.setLinkphone(xmlPullParser.nextText());
}else if("fax".equals(nodeName)){
//设置memo属性
company.setFax(xmlPullParser.nextText());
}else if("address".equals(nodeName)){
company.setAddress(xmlPullParser.nextText());
}else if("mcompanyid".equals(nodeName)){
company.setMcompanyid(xmlPullParser.nextText());
}else if("disorder".equals(nodeName)){
company.setDisorder(xmlPullParser.nextText());
}else if("createtime".equals(nodeName)){
company.setCreatetime(xmlPullParser.nextText());
}else if("createid".equals(nodeName)){
company.setCreateid(xmlPullParser.nextText());
}else if("updateid".equals(nodeName)){
company.setUpdateid(xmlPullParser.nextText());
}else if("updatetime".equals(nodeName)){
company.setUpdatetime(xmlPullParser.nextText());
}
break;
//结束节点
case XmlPullParser.END_TAG:
if("company".equals(nodeName)){
list.add(company);
company=null;
}
break;
default:
break;
}
eventType=xmlPullParser.next();
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
}

return list;
}

}

4调用方法

GetCompanyInfoService service=new GetCompanyInfoService();

service.PullParseXML();返回一个带有List<Company>的集合

遍历集合,取出一个个公司信息

System.out.println(company.getCnname()+";;;"+company.getMcompanyid());

猜你在找的XML相关文章