1

I have a parser to parse the below response,the issue is i am able to parse only first table dataset not able to parse second or later table dataset,not sure where looping is going wrong.the xml response is like

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; }; }; }; }

my parsing code is not parsing entire response properly,The code is

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));
                    }
                }
            }
        }

    }

}

and i am calling from

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);
 }

there is an issue in parsing please help me to correct it.

jenil
  • 141
  • 11
  • When you log `System.out.println("input----> " +input);`, do you have 3 lines, one for each `NewDataSet` array element? – T.Gounelle Mar 15 '15 at 11:34
  • acually i am getting arrayindex exception just after this line,has in you can see my GetReminder class has five fields and index is also coming has five ...., pastebin.com/EngEcQqe – jenil Mar 15 '15 at 11:56
  • can u pls try to parse this using main() taking my response and classes has in pastebin – jenil Mar 15 '15 at 11:57

1 Answers1

0

First, FYI, the response you get is not XML.

You have 2 solutions to properly parse the response:

1- Get the WS response as an actual XML

To do that see this thread. And then use a proper XML DOM parser to process the response and build the Java objects.

2- Do it directly with a SOAP api

Looking for the best way, I came across this with tips that are relevant to your problem. Look at parsing array elements. In your case, you can change your code: Change the line:

KSoap2ResultParser.parseBusinessObject(obj.getProperty(i).toString(),rem);

by:

 KSoap2ResultParser.parseBusinessObject((SoapObject)obj.getProperty(i),rem);

And change the signature of method parseBusinessObject(SoapObject pojoSoap, Object obj). Then in the method, for each type, e.g. for String:

if(strValue.length()!=0){
     fields[i].set(output, strValue);
 }

by:

if(strValue.length()!=0){
     String fieldName = fields[i].getName();
     String fieldValue = pojoSoap.getProperty(fieldName).toString();
     fields[i].set(output, fieldValue);
}
Community
  • 1
  • 1
T.Gounelle
  • 5,953
  • 1
  • 22
  • 32
  • can you please try running my code ,i am making some silly mistake but not sure what in looping in the parser for next values---http://pastebin.com/EngEcQqe,this is giving me arrayIndexOutOfBoundException while Type type=fields[i].getType(); for next data – jenil Mar 15 '15 at 18:26
  • http://pastebin.com/Ugk7Bu8D please let me know seriously i am stuck with this silly mistake – jenil Mar 16 '15 at 08:58
  • @mr-smith i came across this link of your solution i am following the same but causing an issue can u please check http://stackoverflow.com/questions/18997680/how-to-do-the-parsing-of-soap-response – jenil Mar 16 '15 at 18:07
  • any input ?http://stackoverflow.com/questions/29084207/parsing-webservice-anytype-response ,can you try to parse this with looping for multiple entries – jenil Mar 16 '15 at 18:43