0

I am trying to parse through an XML file in Node.js by using the xml2js library. However, I am not able to access the nested contents of that XML file. Here is how my XML file looks like:

<?xml version=\"1.0\" ?>
<ItemSearchResponse
xmlns=\"http://webservices.amazon.com/AWSECommerceService/2013-08-01\">
<OperationRequest>...</OperationRequest>
<Items>
    <Item>
        <LargeImage>..</LargeImage>
    </Item>
    <Item>...</Item>
    <Item>...</Item>
    <Item>...</Item>
</Items>

I am trying to access the LargeImage tag in this xml file. As of now, I am trying to access the sub-nodes by doing this:

        var xml = response.responseBody; //this is an api call which returns the XML file
        parser.parseString(xml, function (err, result) {//parser is an object of xml2js
            console.log(result['ItemSearchResponse']['Items']);
        });
        //console.log("Raw response body: ", response.responseBody);
        }).catch((err) => {
            console.error("Something went wrong! ", err);
        });

And for that this is getting printed on the console:

[ { Request: [ [Object] ],
TotalResults: [ '226308' ],
TotalPages: [ '22631' ],
MoreSearchResultsUrl: 
 [ 'https://www.amazon.in/gp/search?linkCode=xm2&SubscriptionId=AKIAIQWXVVSOIXRWDBIQ&keywords=tshirt&tag=theaedifex-21&creative=165953&url=search-alias%3Daws-amazon-aps&camp=2025' ],
Item: 
 [ [Object],
   [Object],
   [Object],
   [Object],
   [Object],
   [Object],
   [Object],
   [Object],
   [Object],
   [Object] ] } ]

I am trying to access the multiple objects at the end of Items. Any kind of solution or direction to my problem would be really helpful.

  • What happens if you just log `result`? Also, how is this related to the Google Assistant SDK? – Nick Felker May 01 '18 at 16:20
  • @NickFelker Im so sorry, I actually solved the problem after a bit of trial and error. I am currently working on a chatbot that uses the Google Assistant SDK along with DialogFlow for language integration. Thats why I have tagged Google Assistant SDK – Pushkaraj Joshi May 02 '18 at 05:03
  • Are you using Google Assistant SDK or Actions on Google? – Nick Felker May 02 '18 at 17:13

1 Answers1

0

After a bit of trial and error, I changed

result['ItemSearchResponse']['Items']

to

result.ItemSearchResponse.Items[0].Item[0].LargeImage[0].URl

to get the required result.