Ok, so I'm trying to get the eBay item title from the eBay api with the google apps scripts. here is the sample XML output from the url.
<GetSingleItemResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2017-03-08T20:00:54.557Z</Timestamp>
<Ack>Success</Ack>
<Build>E981_CORE_APILW_4424327_R1</Build>
<Version>981</Version>
<Item>
<Description>Description</Description>
<ItemID>132119159999</ItemID>
<EndTime>2017-04-07T00:11:36.000Z</EndTime>
<ViewItemURLForNaturalSearch></ViewItemURLForNaturalSearch>
<ListingType>FixedPriceItem</ListingType>
<Location>Brooklyn, New York</Location>
<GalleryURL>GalleryURL</GalleryURL>
<PictureURL>PictureURL</PictureURL>
<PrimaryCategoryID>181023</PrimaryCategoryID>
<PrimaryCategoryName>Home & Garden:Yard, Garden & Outdoor Living:Gardening Supplies:Composting & Yard Waste:Garden Compost Bins</PrimaryCategoryName>
<BidCount>0</BidCount>
<ConvertedCurrentPrice currencyID="USD">249.99</ConvertedCurrentPrice>
<ListingStatus>Active</ListingStatus>
<TimeLeft>P29DT4H10M42S</TimeLeft>
<Title>Title</Title>
<Country>US</Country>
<AutoPay>false</AutoPay>
<ConditionID>1000</ConditionID>
<ConditionDisplayName>New</ConditionDisplayName>
</Item>
</GetSingleItemResponse>
Here is the code.
function myFunction() {
var item='http://open.api.ebay.com/shopping?callname=GetSingleItem&responseencoding=XML&appid=XXXXXXXXXXXX&siteid=0&version=967&ItemID=132119159999&IncludeSelector=Description';
var xml = UrlFetchApp.fetch(item).getContentText();
var root=XmlService.parse(xml).getRootElement();
var Title=root.getChild('Item').getChild('Title').getText();
Logger.log(Title);
}
After running the code, I'm getting error saying the value is null
"TypeError: Cannot call method "getChild" of null."
You need to pass in the namespace when calling getChild()
:
var ebay = XmlService.getNamespace('urn:ebay:apis:eBLBaseComponents');
var Title=root.getChild('Item',ebay).getChild('Title',ebay).getText();
The namespace is shown by the xmlns
attribute of the root element.
The different forms of getChild()
(and other functions) is documented here:
https://developers.google.com/apps-script/reference/xml-service/element