【问题标题】:Parse XML in google apps script issue eBay api element txt by name按名称解析谷歌应用程序脚本中的 XML 问题 eBay api 元素 txt
【发布时间】:2017-03-08 20:11:09
【问题描述】:

好的,所以我正在尝试使用 google 应用程序脚本从 eBay api 获取 eBay 项目标题。 这是来自 url 的示例 XML 输出。

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

运行代码后,我收到错误提示该值为空

“TypeError:无法调用 null 的方法“getChild”。”

【问题讨论】:

    标签: xml google-apps-script ebay-api


    【解决方案1】:

    调用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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多