XML解析基础02

来源:互联网 发布:访客网络开启安全吗 编辑:程序博客网 时间:2024/05/16 18:45
 
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE Gis_Res SYSTEM "GIS_100.dtd"> <Gis_Res Ver="1.0.0">   <HDA Version="1.0.0">     <SessionID>0510085934000001017405</SessionID>     <Success Message="消息应答成功!" SuccessCode="ResponseIsTheRight"/>   </HDA>   <RGA>     <ReverseGeocodedLocation>       <Point>         <pos isEncrypted="false" isOriginalCoord="false">103.0115111111111 29.979935555555556</pos>       </Point>       <Address countryCode="CN">         <StreetAddress>           <Street>建新路</Street>         </StreetAddress>         <Place type="CountrySubdivision">四川省</Place>         <Place type="Municipality">雅安市</Place>         <Place type="MunicipalitySubdivision">雨城区</Place>         <Place type="AreaCode">511802</Place>       </Address>       <Properties>         <RoadProperty name="roadclass" value="9"/>         <RoadProperty name="name" value="建新路"/>       </Properties>     </ReverseGeocodedLocation>   </RGA> </Gis_Res>     private GisRspInfoModel analyseGisRsp(String responseXML)   throws DocumentException {  GisRspInfoModel model = new GisRspInfoModel();  if (responseXML.indexOf("<!DOCTYPE") > 0) {   int start = responseXML.indexOf("<!DOCTYPE");   int end = responseXML.indexOf(".dtd\">");   String s = responseXML.substring(start, end + 6);   responseXML = responseXML.replaceFirst(s, "");  }  Document document = DocumentHelper.parseText(responseXML);  Element root = document.getRootElement();  Element rga = root.element("RGA");  Element ReverseGeocodedLocation = rga    .element("ReverseGeocodedLocation");  Element address = ReverseGeocodedLocation.element("Address");  Element Properties = ReverseGeocodedLocation.element("Properties");  List<?> lip = Properties.elements("RoadProperty");  for (int i = 0; i < lip.size(); i++) {   Element userElement = (Element) lip.get(i);   Attribute name = userElement.attribute("name");   if ("roadclass".equals(name.getText())) {    Attribute value = userElement.attribute("value");    model.setRLevel(value.getText());   }  }  List<?> list = address.elements("Place");  for (int i = 0; i < list.size(); i++) {   Element childElement = (Element) list.get(i);   Attribute type = childElement.attribute("type");   if ("AreaCode".equals(type.getText())) {    model.setAreaCode(childElement.getText());   }  }  return model; }