parse ksoap 2 result of magento

128 views Asked by At

am trying to parse the response of soapv2 from magento,but I am not able to parse I am getting the result as

catalogCategoryTree{category_id=1; parent_id=0; name=Root; position=1; level=0; children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=3; parent_id=1; name=Root Catalog; is_active=1; position=3; level=1; children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=10; parent_id=3; name=Small Animals; is_active=1; position=10; level=2; children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=22; parent_id=10; name=Living Room; is_active=1; position=22; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=23; parent_id=10; name=Bedroom; is_active=1; position=23; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=45; parent_id=10; name=Pellentesque; is_active=1; position=24; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=46; parent_id=10; name=Facilisis; is_active=1; position=25; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=47; parent_id=10; name=Tortor; is_active=1; position=26; level=3; children=ArrayOfCatalogCategoryEntities{}; }; }; }; item=catalogCategoryEntity{category_id=13; parent_id=3; name=Birds; is_active=1; position=13; level=2; children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=8; parent_id=13; name=Cell Phones; is_active=1; position=8; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=12; parent_id=13; name=Cameras; is_active=1; position=12; level=3; children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=25; parent_id=12; name=Accessories; is_active=1; position=25; level=4; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=26; parent_id=12; name=Digital Cameras; is_active=1; position=26; level=4; children=ArrayOfCatalogCategoryEntities{}; }; }; }; item=catalogCategoryEntity{category_id=15; parent_id=13; name=Computers; is_active=1; position=15; level=3; children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=27; parent_id=15; name=Build Your Own; is_active=1; position=1; level=4; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=28; parent_id=15; name=Laptops; is_active=1; position=2; level=4; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=29; parent_id=13; name=Hard Drives; is_active=1; position=3; level=4; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=30; parent_id=13; name=Monitors; is_active=1; position=4; level=4; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=31; parent_id=13; name=RAM / Memory; is_active=1; position=5; level=4; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=32; parent_id=13; name=Cases; is_active=1; position=6; level=4; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=33; parent_id=13; name=Processors; is_active=1; position=7; level=4; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=34; parent_id=13; name=Peripherals; is_active=1; position=8; level=4; children=ArrayOfCatalogCategoryEntities{}; }; }; }; item=catalogCategoryEntity{category_id=48; parent_id=13; name=Id Dapibus; is_active=1; position=16; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=49; parent_id=13; name=Nam Tempus; is_active=1; position=17; level=3; children=ArrayOfCatalogCategoryEntities{}; }; }; }; item=catalogCategoryEntity{category_id=18; parent_id=3; name=Reptiles & Amphbians; is_active=1; position=18; level=2; children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=4; parent_id=18; name=Shirts; is_active=1; position=4; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=5; parent_id=18; name=Shoes; is_active=1; position=5; level=3; children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=16; parent_id=5; name=Mens; is_active=1; position=16; level=4; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=17; parent_id=5; name=Womens; is_active=1; position=17; level=4; children=ArrayOfCatalogCategoryEntities{}; }; }; }; item=catalogCategoryEntity{category_id=19; parent_id=18; name=Hoodies; is_active=1; position=19; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=24; parent_id=18; name=Pants; is_active=1; position=24; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=50; parent_id=18; name=Professor; is_active=1; position=25; level=3; children=ArrayOfCatalogCategoryEntities{}; }; }; }; item=catalogCategoryEntity{category_id=20; parent_id=3; name=Household Items; is_active=0; position=20; level=2; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=37; parent_id=3; name=Dogs; is_active=1; position=21; level=2; children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=40; parent_id=37; name=Food; is_active=1; position=1; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=41; parent_id=37; name=Toys; is_active=1; position=2; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=42; parent_id=37; name=Apparel; is_active=1; position=3; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=43; parent_id=37; name=Treats; is_active=1; position=4; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=44; parent_id=37; name=Training; is_active=1; position=5; level=3; children=ArrayOfCatalogCategoryEntities{}; }; }; }; item=catalogCategoryEntity{category_id=38; parent_id=3; name=Cats; is_active=1; position=22; level=2; children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=51; parent_id=38; name=Discovered; is_active=1; position=1; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=52; parent_id=38; name=College; is_active=1; position=2; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=53; parent_id=38; name=Lorem Ipsum; is_active=1; position=3; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=54; parent_id=38; name=The cites; is_active=1; position=4; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=55; parent_id=38; name=Passage; is_active=1; position=5; level=3; children=ArrayOfCatalogCategoryEntities{}; }; }; }; item=catalogCategoryEntity{category_id=39; parent_id=3; name=Fish & Aquatic Pets; is_active=1; position=23; level=2; children=ArrayOfCatalogCategoryEntities{item=catalogCategoryEntity{category_id=56; parent_id=39; name=Slightly; is_active=1; position=1; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=57; parent_id=39; name=Believable; is_active=1; position=2; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=58; parent_id=39; name=Embarras; is_active=1; position=3; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=59; parent_id=39; name=Predefined chunks; is_active=1; position=4; level=3; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=60; parent_id=39; name=Necessary; is_active=1; position=5; level=3; children=ArrayOfCatalogCategoryEntities{}; }; }; }; item=catalogCategoryEntity{category_id=61; parent_id=3; name=Boys Toys; is_active=1; position=24; level=2; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=62; parent_id=3; name=Girls Toys; is_active=1; position=25; level=2; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=63; parent_id=3; name=Shop by brand; is_active=1; position=26; level=2; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=64; parent_id=3; name=shop by charactar; is_active=1; position=27; level=2; children=ArrayOfCatalogCategoryEntities{}; }; item=catalogCategoryEntity{category_id=65; parent_id=3; name=shop by age; is_active=1; position=28; level=2; children=ArrayOfCatalogCategoryEntities{}; }; }; }; }; }

I am trying with the below code but no luck

SoapSerializationEnvelope env = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            env.dotNet = false;
            env.xsd = SoapSerializationEnvelope.XSD;
            env.enc = SoapSerializationEnvelope.ENC;

            SoapObject request = new SoapObject(NAMESPACE, "login");

            request.addProperty("username", "guru123");
            request.addProperty("apiKey", "apikey");

            env.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.call("", env);
            Object sessionid = env.getResponse();
            Object result = env.getResponse();

            Log.e("sessionid", sessionid.toString());

            String sessionId_magento = result.toString();

            // for catalog category tree

            request = new SoapObject(NAMESPACE, "catalogCategoryTree");
            request.addProperty("sessionId", sessionId_magento);
            env.setOutputSoapObject(request);
            androidHttpTransport.call("", env);

            Log.e("object", sessionid.toString());

            SoapObject objectResponse;
            SoapObject catalogCategoryTree = (SoapObject) env.getResponse();
            int j = 5;

            for (int i = 0; i <catalogCategoryTree.getPropertyCount(); i++) {
                objectResponse = (SoapObject) catalogCategoryTree.getProperty(j);

                for (int i1 = 0; i < objectResponse.getPropertyCount(); i++) {
                    Object object = objectResponse.getProperty(i);

                    SoapObject soapobjectResponse = (SoapObject) object;



                    SoapObject object2 = (SoapObject) soapobjectResponse.getProperty("children");

                    Object object3 = object2.getProperty(i1);

                    SoapObject object4 = (SoapObject) object3;

                    String categoryName = (String) object4.getProperty("name");

                    SoapObject children = (SoapObject) object4.getProperty("children");
                    listDataHeader.add(object4.getProperty("name").toString());

                    for (int i3= 0; i3 < children.getPropertyCount(); i3++) {
                        SoapObject children1 = (SoapObject) children
                                .getProperty(i3);
                        String childrenname = (String) children1.getProperty("name");

                        int sd = object2.getPropertyCount();

                    }
                }

                j = j + 5;
            }

I am able to get first parent and child EX : Parent name=Small Animals, Child name = Living Room etc but i could not iterate through next parent and child to get Parent name= Birds Child name = Cell Phones etc.

2

There are 2 answers

0
Javasamurai On

Create another request with the deeper level of tree and get the response.

SoapObject request=new SoapObject(NAMESPACE,"catalogCategoryTree");
request.addProperty("sessionId",key);
request.addProperty("parentId",level);
0
Ankur On

Well, I stuck with the same issue, and finally wrote a simple code to convert SOAP response to JSON:

public static String soapStrToJson(String input)
{
    String output;

    output = input;
    output = output.replace("key=", "\"");
    output = output.replace("; value=", "\":");
    output = output.replace("; };",",");
    output = output.replace("item=anyType{","");
    output = output.replace("Map","");
    output = output.replace(",}","}");
    output = output.replace(", }"," }");
    output = output.replace("null","0");

    Pattern pattern = Pattern.compile(":(.*?),");
    Matcher matcher = pattern.matcher(output);

    List<String> wordsToReplace = new ArrayList();
    while (matcher.find())
    {
        String str = matcher.group(0);
        if(str.contains("{") || str.contains("["))
            continue;

        str = str.replace(":", "");
        str = str.replace(",", "");

        if(str.matches("^-?\\d+$"))
            continue;

        System.out.println("-->" + str);
        wordsToReplace.add(str);
    }

    for(String str : wordsToReplace)
    {
        output = output.replace(":"+str+",", ":\""+str+"\",");
    }
    return output;
}

and used this function as:

parseCategoryJSON( new JSONObject( soapStrToJson( env.getResponse().toString() ) ) )

Note that I am not using env.bodyIn SoapObject.

This may not be the perfect converter for all responses, so modify 'String.replace' and Pattern-Matchers as per your requirement.