XML获取子节点时

发布于 2025-02-05 23:39:05 字数 2192 浏览 2 评论 0原文

试图访问第一项的孩子时,无效的结果。我正在使用Google App脚本。

function parseXml() {
  var url = 'https://example.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();

  var channel = root.getChild('channel');
  var items = channel.getChildren('item');
  Logger.log(items[1].getValue());
  Logger.log(items[1].getChildren())
  Logger.log(items[1].getChild('g:id'))

}

输出:

5:37:46 PM  Notice  Execution started
5:37:57 PM  Info    09771332150202100001A&C Ainsworth Wines - A&C Ainsworth Wines - 2021
5:37:57 PM  Info    [[Element: <g:id [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:title [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:description [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:link [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:image_link [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:availability [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:price [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:item_group_id [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:brand [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:gender [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:condition [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:google_product_category [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:product_type [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:spec [Namespace: http://base.google.com/ns/1.0]/>], [Element: <quantity/>], [Element: <option_values/>]]
5:37:57 PM  Info    null
5:37:58 PM  Notice  Execution completed

最后,XML示例。抱歉,我无法共享源URL。

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
 <item>
  <g:id>09771332140202100001</g:id>
   <g:title>A&C Ainsworth Wines - A&C Ainsworth Wines - 2021</g:title>
   <g:description></g:description>
 </item>

Null results when trying to access children of the first item. I'm using Google App Scripts.

function parseXml() {
  var url = 'https://example.xml';
  var xml = UrlFetchApp.fetch(url).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();

  var channel = root.getChild('channel');
  var items = channel.getChildren('item');
  Logger.log(items[1].getValue());
  Logger.log(items[1].getChildren())
  Logger.log(items[1].getChild('g:id'))

}

Output:

5:37:46 PM  Notice  Execution started
5:37:57 PM  Info    09771332150202100001A&C Ainsworth Wines - A&C Ainsworth Wines - 2021
5:37:57 PM  Info    [[Element: <g:id [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:title [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:description [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:link [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:image_link [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:availability [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:price [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:item_group_id [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:brand [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:gender [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:condition [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:google_product_category [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:product_type [Namespace: http://base.google.com/ns/1.0]/>], [Element: <g:spec [Namespace: http://base.google.com/ns/1.0]/>], [Element: <quantity/>], [Element: <option_values/>]]
5:37:57 PM  Info    null
5:37:58 PM  Notice  Execution completed

Finally, XML example. Sorry, I can't share the source URL.

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
 <item>
  <g:id>09771332140202100001</g:id>
   <g:title>A&C Ainsworth Wines - A&C Ainsworth Wines - 2021</g:title>
   <g:description></g:description>
 </item>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰ツ都不明白 2025-02-12 23:39:05

我相信您的当前问题和您的目标如下。

  • logger.log(项目[1] .getChild('g:id'))返回null
  • 您需要从&lt; g:id&gt; ###&lt;/g:id in in item中通过修改脚本来检索值。

当我在脚本中看到您的显示XML和您的脚本时,我认为必须使用名称空间。当这反映在您的脚本中时,如下所示。

来自:

Logger.log(items[1].getChild('g:id'))

到:

Logger.log(items[1].getChild('id', XmlService.getNamespace("http://base.google.com/ns/1.0")))

Logger.log(items[1].getChild('id', XmlService.getNamespace("g", "http://base.google.com/ns/1.0")))

Logger.log(items[1].getChild('id', XmlService.getNamespace("http://base.google.com/ns/1.0")).getValue())

或或

Logger.log(items[1].getChild('id', XmlService.getNamespace("g", "http://base.google.com/ns/1.0")).getValue())
  • 不确定您的实际XML数据,但我无法测试,但我认为在您的XML数据中,logger.log(项目[1] .getChild(' id',xmlService.getNamespace(“ http://base.google.com/ns/1.0”))。getValue())可能有效。

参考:

I believe your current issue and your goal are as follows.

  • Logger.log(items[1].getChild('g:id')) returns null.
  • You want to retrieve the value from <g:id>###</g:id> in item by modifying your script.

When I saw your showing XML and your script, in your script, I thought that the namespace is required to be used. When this is reflected in your script, it becomes as follows.

From:

Logger.log(items[1].getChild('g:id'))

To:

Logger.log(items[1].getChild('id', XmlService.getNamespace("http://base.google.com/ns/1.0")))

or

Logger.log(items[1].getChild('id', XmlService.getNamespace("g", "http://base.google.com/ns/1.0")))

or

Logger.log(items[1].getChild('id', XmlService.getNamespace("http://base.google.com/ns/1.0")).getValue())

or

Logger.log(items[1].getChild('id', XmlService.getNamespace("g", "http://base.google.com/ns/1.0")).getValue())
  • Although I'm not sure about your actual XML data and I cannot test this, I thought that in the case of your XML data, Logger.log(items[1].getChild('id', XmlService.getNamespace("http://base.google.com/ns/1.0")).getValue()) might work.

Reference:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文