2017-03-08 30 views
1

好的,所以我試圖從eBay api中獲取eBay項目標題與谷歌應用程序腳本。 這裏是來自url的示例XML輸出。在google apps腳本中解析XML按名稱發佈eBay api元素txt

<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 &amp; Garden:Yard, Garden &amp; Outdoor Living:Gardening Supplies:Composting &amp; 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> 

這是代碼。

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); 
} 

運行代碼後,我收到提示說值爲null

「類型錯誤:無法調用‘getChild’空的」。

回答

0

需要在命名空間來傳遞呼叫getChild()時:

var ebay = XmlService.getNamespace('urn:ebay:apis:eBLBaseComponents'); 
var Title=root.getChild('Item',ebay).getChild('Title',ebay).getText(); 

命名空間由根元素的屬性xmlns所示。

不同形式的getChild()(和其他功能)記錄在這裏:

https://developers.google.com/apps-script/reference/xml-service/element

+0

太謝謝你了!解決了這個問題! – KChan