Search code examples
xmlxpathslash

Converting XML to JSON object where XML node closing contains both front/back from Server?


I am getting this data from SOAP Service where in XML data in placed in JSON's GetclientStoreResult as XML but every XML node closing contains front as well as back slash. How could i parse this and convert it in jsonobject.

{
       "GetclientStoreResult": 
       "<Response>
                <StatusCode>200<\/StatusCode>
                <StoresCount>3<\/StoresCount>
                <Stores>

        <StoreDetails>
            <StoreName>client Store<\/StoreName>
            <AddressLine1>Fatehabad Road<\/AddressLine1>
            <AddressLine2>Khasra No.-76, Mauja Rajpur, Near Amar Hotel, Fatehabad Road, Agra<\/AddressLine2>
            <City>Agra<\/City>
            <State>Uttar Pradesh<\/State>
            <countryCode>IN<\/countryCode>
            <Postalcode>282001<\/Postalcode>
            <MainPhone>09x 1x 0x71x0<\/MainPhone>
            <MobilePhone>09x 1x9 x97x9x<\/MobilePhone>
            <MobilePhone2><\/MobilePhone2>
            <HomePage>http:\/\/www.client.in<\/HomePage>
            <EMail>clientcare.upw@client.com<\/EMail>
            <Timings>1:10:30:19:30,2:10:30:19:30,3:10:30:19:30,4:10:30:19:30,5:10:30:19:30,6:10:30:19:30,7:10:30:19:30<\/Timings>
            <PaymentType><\/PaymentType>
            <Category>Mobile Service Provider Company<\/Category>
            <Description>Service & Sales Store for client<\/Description>
            <Latitude>27.164696<\/Latitude>
            <Longitude>78.038031<\/Longitude>
            <Distance>1.21778962564819<\/Distance>
        <\/StoreDetails>

        <StoreDetails>
            <StoreName>client Store<\/StoreName>
            <AddressLine1>VS Pratap Pura<\/AddressLine1>
            <AddressLine2>client Store G1 & G2 Ground floor Hotel Usha Kiran Complex Pratap Pura MG Road Agra<\/AddressLine2>
            <City>Agra<\/City>
            <State>Uttar Pradesh<\/State>
            <countryCode>IN<\/countryCode>
            <Postalcode>282001<\/Postalcode>
            <MainPhone>09x 19 x971x0<\/MainPhone>
            <MobilePhone>0x7 19 x7190<\/MobilePhone>
            <MobilePhone2><\/MobilePhone2>
            <HomePage>http:\/\/www.client.in<\/HomePage>
            <EMail>clientcare.upw@client.com<\/EMail>
            <Timings>1:10:30:19:30,2:10:30:19:30,3:10:30:19:30,4:10:30:19:30,5:10:30:19:30,6:10:30:19:30,7:10:30:19:30<\/Timings>
            <PaymentType><\/PaymentType>
            <Category>Mobile Service Provider Company<\/Category>
            <Description>Service & Sales Store for client<\/Description>
            <Latitude>27.15356<\/Latitude>
            <Longitude>78.007565<\/Longitude>
            <Distance>4.1715187443831<\/Distance>
        <\/StoreDetails>

        <StoreDetails>
            <StoreName>client Store<\/StoreName>
            <AddressLine1>Sanjay Place<\/AddressLine1>
            <AddressLine2>Shop No. G-1, Block No. 38\/4B, Sanjay Place, Agra<\/AddressLine2>
            <City>Agra<\/City>
            <State>Uttar Pradesh<\/State>
            <countryCode>IN<\/countryCode>
            <Postalcode>282002<\/Postalcode>
            <MainPhone>0x7 1x 097x90<\/MainPhone>
            <MobilePhone>0x7 19 0xx190<\/MobilePhone>
            <MobilePhone2><\/MobilePhone2>
            <HomePage>http:\/\/www.client.in<\/HomePage>
            <EMail>clientcare.upw@client.com<\/EMail>
            <Timings>1:10:30:19:30,2:10:30:19:30,3:10:30:19:30,4:10:30:19:30,5:10:30:19:30,6:10:30:19:30,7:10:30:19:30<\/Timings>
            <PaymentType><\/PaymentType>
            <Category>Mobile Service Provider Company<\/Category>
            <Description>Service & Sales Store for client<\/Description>
            <Latitude>27.198541<\/Latitude>
            <Longitude>78.006023<\/Longitude>
            <Distance>4.4289447148507<\/Distance>
        <\/StoreDetails>

        <\/Stores>
<\/Response>",


"isSuccessful": true,
   "xmlns": "http:\/\/www.client.in\/StoreLocator"
}

Solution

  • Finally, i done sort off workaround i could say.

    1. Converted Server response to text i.e "Yo, Maria</Somedata>".
    2. Replace '\' with ''(blank). i.e. " Yo, Maria < /Somedata>"
    3. Replace '"' with '' i.e extra double quotes from text data, Yo, Maria < /Somedata>
    4. $.parseXML( Yo, Maria < /Somedata>);
    5. Converted parsed xml to jsonObject and then used it.

    Please find actual code for doing it...

    var xmlRawData = JSON.stringify(response.invocationResult.GetVodafoneStoreResult);
        var xmlData = xmlRawData.replace(/\\/g, ' ');
        var xmlDataTrim = xmlData.replace(/\"/g, '');
        //var str= xmlData;
        var xmlDoc = $.parseXML(xmlDataTrim);
        var jsonObj = xmlToJson(xmlDoc);
    
    function xmlToJson(xml) {
    
        // Create the return object
        var obj = {};
    
        if (xml.nodeType == 1) { // element
            // do attributes
            if (xml.attributes.length > 0) {
            obj["@attributes"] = {};
                for (var j = 0; j < xml.attributes.length; j++) {
                    var attribute = xml.attributes.item(j);
                    obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
                }
            }
        } else if (xml.nodeType == 3) { // text
            obj = xml.nodeValue;
        }
    
        // do children
        if (xml.hasChildNodes()) {
            for(var i = 0; i < xml.childNodes.length; i++) {
                var item = xml.childNodes.item(i);
                var nodeName = item.nodeName;
                if (typeof(obj[nodeName]) == "undefined") {
                    obj[nodeName] = xmlToJson(item);
                } else {
                    if (typeof(obj[nodeName].push) == "undefined") {
                        var old = obj[nodeName];
                        obj[nodeName] = [];
                        obj[nodeName].push(old);
                    }
                    obj[nodeName].push(xmlToJson(item));
                }
            }
        }
        return obj;
    };