xml的解析
2016-07-26 17:08
603 查看
private void getLayer(){
try{
String result = WebServiceUtils.getMapLayers();
InputStream inputStream = new ByteArrayInputStream(result.getBytes());
LayerInfo.parse(inputStream);
}catch(Exception e){
e.printStackTrace();
}
}
public static String getMapLayers(){
String result ="";
try{
String endPoint = AppContext.url + "/ZZMobileService.asmx";
String methodName ="getMapLayers";
String soapAction = NAMESPACE + methodName;
SoapObject rpc = new SoapObject(NAMESPACE, methodName);
rpc.addProperty("checkCode", PWD);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
HttpTransportSE transport = new HttpTransportSE(endPoint);
transport.call(soapAction, envelope);
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
result = object.getProperty(0).toString();
}catch(Exception e){
e.printStackTrace();
}
Log.i("TAG", "result="+result);
return result;
}
public static void parse(InputStream inputStream) throws IOException{
AppContext.pLayerList.removeAll(AppContext.pLayerList);
AppContext.cLayerList.removeAll(AppContext.cLayerList);
LayerInfo info = null;
XmlPullParser xmlParser = Xml.newPullParser();
try{
xmlParser.setInput(inputStream, "UTF-8");
int evtType=xmlParser.getEventType();
while(evtType!=XmlPullParser.END_DOCUMENT){
String tag = xmlParser.getName();
switch (evtType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
if(tag.equalsIgnoreCase("layer")){
info = new LayerInfo();
String pid = xmlParser.getAttributeValue(null, "pid");
if(pid ==null){
info.setName(xmlParser.getAttributeValue(null, "name"));
info.setChecked(xmlParser.getAttributeValue(null, "checked"));
info.setIsShow(xmlParser.getAttributeValue(null, "isShow"));
info.setLayerUrl(xmlParser.getAttributeValue(null, "url"));
info.setType(xmlParser.getAttributeValue(null,"type"));
AppContext.pLayerList.add(info);
}
}else if(tag.equalsIgnoreCase("clayer")){
if(info!=null){
ChildLayerInfo child = new ChildLayerInfo();
child.setId(xmlParser.getAttributeValue(null,"id"));
child.setName(xmlParser.getAttributeValue(null,"name"));
child.setType(xmlParser.getAttributeValue(null,"type"));
info.addChildInfo(child);
}
}
break;
case XmlPullParser.END_TAG:
break;
}
evtType=xmlParser.next();
}
}catch(Exception e){
e.printStackTrace();
}finally{
inputStream.close();
}
}
try{
String result = WebServiceUtils.getMapLayers();
InputStream inputStream = new ByteArrayInputStream(result.getBytes());
LayerInfo.parse(inputStream);
}catch(Exception e){
e.printStackTrace();
}
}
public static String getMapLayers(){
String result ="";
try{
String endPoint = AppContext.url + "/ZZMobileService.asmx";
String methodName ="getMapLayers";
String soapAction = NAMESPACE + methodName;
SoapObject rpc = new SoapObject(NAMESPACE, methodName);
rpc.addProperty("checkCode", PWD);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
HttpTransportSE transport = new HttpTransportSE(endPoint);
transport.call(soapAction, envelope);
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
result = object.getProperty(0).toString();
}catch(Exception e){
e.printStackTrace();
}
Log.i("TAG", "result="+result);
return result;
}
public static void parse(InputStream inputStream) throws IOException{
AppContext.pLayerList.removeAll(AppContext.pLayerList);
AppContext.cLayerList.removeAll(AppContext.cLayerList);
LayerInfo info = null;
XmlPullParser xmlParser = Xml.newPullParser();
try{
xmlParser.setInput(inputStream, "UTF-8");
int evtType=xmlParser.getEventType();
while(evtType!=XmlPullParser.END_DOCUMENT){
String tag = xmlParser.getName();
switch (evtType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
if(tag.equalsIgnoreCase("layer")){
info = new LayerInfo();
String pid = xmlParser.getAttributeValue(null, "pid");
if(pid ==null){
info.setName(xmlParser.getAttributeValue(null, "name"));
info.setChecked(xmlParser.getAttributeValue(null, "checked"));
info.setIsShow(xmlParser.getAttributeValue(null, "isShow"));
info.setLayerUrl(xmlParser.getAttributeValue(null, "url"));
info.setType(xmlParser.getAttributeValue(null,"type"));
AppContext.pLayerList.add(info);
}
}else if(tag.equalsIgnoreCase("clayer")){
if(info!=null){
ChildLayerInfo child = new ChildLayerInfo();
child.setId(xmlParser.getAttributeValue(null,"id"));
child.setName(xmlParser.getAttributeValue(null,"name"));
child.setType(xmlParser.getAttributeValue(null,"type"));
info.addChildInfo(child);
}
}
break;
case XmlPullParser.END_TAG:
break;
}
evtType=xmlParser.next();
}
}catch(Exception e){
e.printStackTrace();
}finally{
inputStream.close();
}
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories