xml的解析

本文介绍了一种通过调用WebService服务来获取地图图层信息的方法。具体实现了发送SOAP请求并解析返回的XML数据,将图层及其子图层信息存储到列表中,以便进一步处理或显示。


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();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值