#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    4
    Rep Power
    0

    SOAP XML to Json


    I am trying to parse a SOAP xml reply in javascript. I am using xml2js-parser (parseSynch method) but the SOAP format may be throwing it off.

    Here's my response I am trying to parse:

    Code:
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Body>
    <GetTrackerDataResponse xmlns="http://www.coolsite.com/message/">
    <Version>1.5</Version>
    <Query>
    <Phone>5555555555</Phone>
    </Query>
    <AsOf>2018-02-01T10:50:29</AsOf>
    <OrderStatuses/>
    </GetTrackerDataResponse>
    </soap:Body>
    </soap:Envelope>
    I got it to a json object, however, some of the objects appear as arrays of length 1 instead of just the value. I want it in this form:

    Code:
    {
    	"soap:body":{
    		'GetTrackerDataResponse xmlns="http://www.coolsite.com/message/"':{
    			"Version":"1.5",
    			"Query":{
    				"Phone":"5555555"
    			},
    			"Asof":"2018-02-01T10:50:29"
    		}		
    	}
    }
    I am getting somthing like this:
    Code:
    {
    	"soap:body":[{
    		'GetTrackerDataResponse xmlns="http://www.coolsite.com/message/"':{
    			"Version":"[1.5"],
    			"Query":[{
    				"Phone":["5555555"]
    			}],
    			"Asof":["2018-02-01T10:50:29"]
    		}]		
    	}
    }
    Any help is very much appreciated. The response changes (obviously), and it just gets more complex from here. As orders get added there is a lot more information to parse through.

    Thanks!
    neech
    ------------------------------------------------------------------------------
    "People think it don't be like it be....but it do" - T-Pain
  2. #2
  3. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,915
    Rep Power
    9646
    That format is weird.

    You don't need a third-party library to read XML.
    javascript Code:
    var doc = new DOMParser().parseFromString(response, "application/xml");

    From there it depends what you want to do with the XML...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    4
    Rep Power
    0
    Thanks for the reply requinix. I should have mentioned before that I am coding in node.js not within a web page. I still can use a DOMParser, but it would be a third party as well.

    This is my first experience trying to parse a "SOAP" format xml...which is why I need a fancy script to do it. Yes, the format is all weird.

    Any other ideas? In the meantime I will try a third party DOMParser.

    Thanks,
    neech
  6. #4
  7. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,915
    Rep Power
    9646
    The format is weird (why is the xmlns included in the key?) but as long as it's consistently weird you can work with it.

    I still need to know what you want to do with the output. The format you're getting isn't what you're expecting but you should be able to use it just fine:
    Code:
    variable["soap:body"]
      [0] // annoying but easy enough to add in
      ['GetTrackerDataResponse xmlns="http://www.coolsite.com/message/"'] /* same as before. but weird */
      .Query
      [0] // also easy
      .Phone
    Basically all you have to do is add in a couple [0]s.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    4
    Rep Power
    0
    The end goal is just to get the array of the <orderstatuses> object and track updates on the orders. Send them an email, or whatever.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    4
    Rep Power
    0
    I want the values in the order array not to be in another array either...
  12. #7
  13. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,915
    Rep Power
    9646
    Okay, well, the example you posted doesn't have any <OrderStatuses> in it. Do you have an example that does? And the example I gave on how to access stuff in the object, does that help or do you have any questions about it?

IMN logo majestic logo threadwatch logo seochat tools logo