我有一个解析器来解析下面的响应,问题是我只能解析第一个不能解析第二个或更晚的表数据集的表数据集,不知道循环出错的地方.xml响应就像
anyType{schema=anyType{element=anyType{complexType=anyType{choice=anyType{element=anyType{complexType=anyType{sequence=anyType{element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; element=anyType{}; }; }; }; }; }; }; }; diffgram=anyType{NewDataSet=anyType{Table=anyType{RemMessage=Exeed Discount Limit on Invoice dated on 05/03/2015 for C SHAH , from 3 - Lokhandwala Showroom; InvM_Id=77693; DocType=3; PrmR_TypeId=3; PrmR_Id=1820; }; **Table=anyType{RemMessage=Exeed Discount Limit on Invoice dated on 14/03/2015 for G P SHAH , from 3 - Khar Showroom; InvM_Id=77800; DocType=3; PrmR_TypeId=3; PrmR_Id=1865; };** Table=anyType{RemMessage=Exeed Discount Limit on Invoice dated on 14/03/2015 for DOONGARSHI SHAH , from 3 - Khar Showroom; InvM_Id=77801; DocType=3; PrmR_TypeId=3; PrmR_Id=1866; }; }; }; }
我的解析代码没有正确解析整个响应,代码是
public class KSoap2ResultParser {
public static void parseBusinessObject(String input, Object output) throws NumberFormatException, IllegalArgumentException, IllegalAccessException, InstantiationException{
System.out.println("input----> " +input);
Class theClass = output.getClass();
Field[] fields = theClass.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Type type=fields[i].getType();
System.out.println("type--" +type);
fields[i].setAccessible(true);
//detect String
if (fields[i].getType().equals(String.class)) {
String tag = fields[i].getName() + "="; //"s" is for String in the above soap response example + field name for example Name = "sName"
System.out.println("fff------------"+tag);
if(input.contains(tag)){
String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag)));
System.out.println("RemMessage------------"+strValue);
if(strValue.length()!=0){
fields[i].set(output, strValue);
}
}
}
//detect int or Integer
if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
String tag = fields[i].getName() + "="; //"i" is for Integer or int in the above soap response example+ field name for example Goals = "iGoals"
if(input.contains(tag)){
String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag)));
System.out.println("strvalue------------"+strValue);
if(strValue.length()!=0){
fields[i].setInt(output, Integer.valueOf(strValue));
}
}
}
//detect float or Float
if (type.equals(Float.TYPE) || type.equals(Float.class)) {
String tag = "f" + fields[i].getName() + "=";
if(input.contains(tag)){
String strValue = input.substring(input.indexOf(tag)+tag.length(), input.indexOf(";", input.indexOf(tag)));
if(strValue.length()!=0){
fields[i].setFloat(output, Float.valueOf(strValue));
}
}
}
}
}
}
我打电话给
String response=androidHttpTransport.responseDump;
SoapObject obj=(SoapObject)envelope.getResponse();
for(int i=0; i < obj.getPropertyCount(); i++) {
GetReminder rem = new GetReminder();
KSoap2ResultParser.parseBusinessObject(obj.getProperty(i).toString(),rem);
reminders.add(rem);
}
解析时出现问题,请帮我纠正。
答案 0 :(得分:0)
首先,仅供参考,您获得的回复是不是 XML。
您有2个解决方案来正确解析响应:
要做到这一点,请参阅this thread。然后使用适当的XML DOM parser来处理响应并构建Java对象。
寻找最好的方法,我遇到this,其中包含与您的问题相关的提示。 看看parsing array elements。在您的情况下,您可以更改您的代码: 改变这一行:
KSoap2ResultParser.parseBusinessObject(obj.getProperty(i).toString(),rem);
由:
KSoap2ResultParser.parseBusinessObject((SoapObject)obj.getProperty(i),rem);
并更改方法parseBusinessObject(SoapObject pojoSoap, Object obj)
的签名。然后在该方法中,对于每种类型,例如, String
:
if(strValue.length()!=0){ fields[i].set(output, strValue); }
由:
if(strValue.length()!=0){
String fieldName = fields[i].getName();
String fieldValue = pojoSoap.getProperty(fieldName).toString();
fields[i].set(output, fieldValue);
}