解析SOAP响应中的非法属性错误

问题描述:

我正在研究一个利用我的Web服务获取数据并显示它的android应用程序。我的SOAP响应有点复杂,看起来像这样。解析SOAP响应中的非法属性错误

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
<GetSessionDetailsResponse xmlns="https://viberservices.com/gcdev/"> 
    <GetSessionDetailsResult> 
    <ModuleFlags>string</ModuleFlags> 
    <SessionID>string</SessionID> 
    <UserInfo> 
     <Fullname>string</Fullname> 
     <Language>long</Language> 
    </UserInfo> 
    <Locations> 
     <LocationDetails> 
     <LocationID>long</LocationID> 
     <LocationName>string</LocationName> 
     <PhotoURL>string</PhotoURL> 
     </LocationDetails> 
     <LocationDetails> 
     <LocationID>long</LocationID> 
     <LocationName>string</LocationName> 
     <PhotoURL>string</PhotoURL> 
     </LocationDetails> 
    </Locations> 
    </GetSessionDetailsResult> 
</GetSessionDetailsResponse> 

现在我想从位置部分的位置ID和地点名称的数据。我正在使用下面的代码。

SoapObject res=(SoapObject)envelope.bodyIn; 
SoapObject t=(SoapObject)res.getProperty("Locations"); 
for(int i=0; i<t.getPropertyCount(); i++){ 
    SoapObject locinfo=(SoapObject)t.getProperty(i); 
    String lid= locinfo.getProperty("LocationID").toString(); 
    String lname= locinfo.getProperty("LocationName").toString(); 
} 
//Code to display lid and lname 

但我得到一个运行时异常说了java.lang.RuntimeException:非法财物:位置

我设法通过使用下面的代码来解决这个问题:

SoapObject res=(SoapObject)envelope.bodyIn; 
SoapObject root=(SoapObject)((SoapObject)res.getProperty("GetSessionDetailsResult")).getProperty("Locations"); 
for(int i=0; i<root.getPropertyCount(); i++){ 
    SoapObject t=(SoapObject)root.getProperty(i); 
    String lid= t.getProperty("LocationID").toString(); 
    String lname= t.getProperty("LocationName").toString(); 
} 

的Android SAX解析器:

SAXXMLParser:

public class SAXXMLParser { 

public static List<Location> parse(String input) { 
    List<Location> locations = null; 

    try { 
     // create a XMLReader from SAXParser 
     XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser() 
       .getXMLReader(); 
     // create a SAXXMLHandler 
     SAXXMLHandler saxHandler = new SAXXMLHandler(); 
     // store handler in XMLReader 
     xmlReader.setContentHandler(saxHandler); 
     // the process starts 
     xmlReader.parse(input); 
     // get the `Locations list` 
     locations = saxHandler.getEmployees(); 

    } catch (Exception ex) { 
     Log.d("XML", "SAXXMLParser: parse() failed"); 
    } 

    // return location list 
    return locations; 
} 

} 

SAXXMLHandler:

import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 

import java.util.ArrayList; 
import java.util.List; 


public class SAXXMLHandler extends DefaultHandler { 


private List<Location> locations; 
private String tempVal; 
// to maintain context 
private Location location; 

public SAXXMLHandlerTest() { 
    locations = new ArrayList<Location>(); 
} 

public List<Location> getEmployees() { 
    return locations; 
} 

// Event Handlers 
public void startElement(String uri, String localName, String qName, 
         Attributes attributes) throws SAXException { 
    // reset 
    tempVal = ""; 
    if (qName.equalsIgnoreCase("LocationDetails")) { 
     // create a new instance of location 
     location = new Location(); 
    } 
} 

public void characters(char[] ch, int start, int length) 
     throws SAXException { 
    tempVal = new String(ch, start, length); 
} 

public void endElement(String uri, String localName, String qName) 
     throws SAXException { 
    if (qName.equalsIgnoreCase("LocationDetails")) { 
     // add it to the list 
     locations.add(location); 
    } else if (qName.equalsIgnoreCase("LocationID")) { 
     location.setLocationID(Long.parseLong(tempVal)); 
    } else if (qName.equalsIgnoreCase("LocationName")) { 
     location.setLocationName(tempVal); 
    } else if (qName.equalsIgnoreCase("PhotoURL")) { 
     location.setPhotoURL(tempVal); 
    } 
} 

} 

模型类:

public class Location { 


private long LocationID; 
private String LocationName; 
private String PhotoURL; 

public long getLocationID() { 
    return LocationID; 
} 

public void setLocationID(long locationID) { 
    LocationID = locationID; 
} 

public String getLocationName() { 
    return LocationName; 
} 

public void setLocationName(String locationName) { 
    LocationName = locationName; 
} 

public String getPhotoURL() { 
    return PhotoURL; 
} 

public void setPhotoURL(String photoURL) { 
    PhotoURL = photoURL; 
} 

} 

使用此代码在您的活动/片段来分析数据

List<Location> locations = SAXXMLParsertest.parse("Your xml data"); 

    if (locations != null && locations.size() > 0) { 

     for (int i = 0; i < locations.size(); i++) { 

      Log.e("Location name", " " + locations.get(i).getLocationName()); 
      Log.e("Location Id", " " + locations.get(i).getLocationID()); 
      Log.e("Photo Uri", " " + locations.get(i).getPhotoURL()); 

     } 
    }