解析 SOAP 响应时出现非法 属性 错误
Illegal Property error in parsing SOAP response
我正在开发一个 android 应用程序,它利用我的网络服务来获取数据并显示它。我的 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>
现在我想从 Location 部分获取 Location ID 和 Location Name 数据。我正在使用以下代码。
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: 非法 属性: Locations
Android 萨克斯解析器:
SAXML 解析器:
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;
}
}
SAXML 处理器:
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);
}
}
}
型号class:
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;
}
}
在您的 activity/fragment 中使用此代码解析数据
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());
}
}
我使用以下代码成功克服了这个问题:
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 应用程序,它利用我的网络服务来获取数据并显示它。我的 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>
现在我想从 Location 部分获取 Location ID 和 Location Name 数据。我正在使用以下代码。
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: 非法 属性: Locations
Android 萨克斯解析器:
SAXML 解析器:
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;
}
}
SAXML 处理器:
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);
}
}
}
型号class:
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;
}
}
在您的 activity/fragment 中使用此代码解析数据
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());
}
}
我使用以下代码成功克服了这个问题:
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();
}