gpt4 book ai didi

xml - 使用 XmlService 解析时如何从字典 API 获取子元素

转载 作者:行者123 更新时间:2023-11-28 00:58:49 24 4
gpt4 key购买 nike

我正在创建一个自定义函数来从字典 API 中提取定义。我一直遇到不让我捕获 child 的问题。我可以获得根元素,但之后就没有骰子了。

据我所知,XML 不再有效。需要使用XmlService
https://developers.google.com/apps-script/reference/xml-service/

我想选择要提取的定义。

DEF_ELEMENTARY("word", 1, 1) 应该返回:

":a sound or combination of sounds that has meaning and is spoken by a human being"

DEF_ELEMENTARY("word",2,1) 应该返回:

":to express in words :"

xpath 应该是这样的。还没来得及测试。
//entry[0]/def/dt[0]>

function DEF_ELEMANTARY(input, entryIndex, defIndex) {

if (!input) {
input = "test";
}

/* test if they added indexing values. You subtract 1 so the function is more human readable */
if (!entryIndex) {
entryIndex = 0;
} else {
entryIndex = entryIndex - 1
}

if (!defIndex) {
defIndex = 0;
} else {
defIndex = defIndex - 1
}


/*api only accepts lowercase words*/
var word = input.toLowerCase();

var apiKey = "*-*-*-*-*"
var url = "http://www.dictionaryapi.com/api/v1/references/sd2/xml/"+word+"?key="+apiKey

var fetch = UrlFetchApp.fetch(url).getContentText();
var doc = XmlService.parse(fetch);

var root = doc.getRootElement();

var entries = root.getChildren("Entry");

return entries;
/*this is where I get lost. If I do any methods to the entries it says it isn't defined
the editor normally suggests methods and instead of the normal Element methods it is
the generic ones*/
}

这是我从 UrlFetch 获取的 XML:

"<?xml version=""1.0"" encoding=""utf-8"" ?>
<entry_list version=""1.0"">
<entry id=""word[1]""><hw hindex=""1"">word</hw><sound><wav>word0001.wav</wav></sound><pr>ˈwərd</pr><fl>noun</fl><def><sn>1</sn><dt>:a sound or combination of sounds that has meaning and is spoken by a human being</dt><sn>2</sn><dt>:a written or printed letter or letters standing for a spoken word</dt><sn>3</sn><dt>:a brief remark or conversation <vi>I'd like a <it>word</it> with you.</vi></dt><sn>4</sn><dt>:<sx>command <sxn>1</sxn></sx> <sx>order</sx> <vimw>We are waiting for the <it>word</it> to begin.</vimw></dt><sn>5</sn><dt>:<sx>news <sxn>1</sxn></sx> <vi>Has there been any <it>word</it> on how they are?</vi></dt><sn>6</sn><dt>:<sx>promise <sxn>1</sxn></sx> <vi>I give you my <it>word</it>.</vi></dt><sn>7</sn><sin><if>words</if> <spl>plural</spl></sin><dt>:remarks said in anger or in a quarrel <vi>We had <it>words</it> yesterday.</vi></dt></def></entry>
<entry id=""word[2]""><hw hindex=""2"">word</hw><sound><wav>word0001.wav</wav></sound><fl>verb</fl><in><if>word*ed</if></in><in><if>word*ing</if></in><def><dt>:to express in words :<sx>phrase</sx> <vimw>I don't know how to <it>word</it> my idea.</vimw></dt></def></entry>
<entry id=""word processing""><hw>word processing</hw><fl>noun</fl><def><dt>:the production of printed documents (as business letters) with automated and usually computerized equipment</dt></def></entry>
<entry id=""word processor""><hw>word processor</hw><fl>noun</fl><def><sn>1</sn><dt>:a computer used for creating, storing, and printing text</dt><sn>2</sn><dt>:software designed to perform word processing</dt></def></entry>
<entry id=""guide word""><hw>guide word</hw><fl>noun</fl><def><dt>:either of the terms at the head of a page of an alphabetical reference work (as a dictionary) usually showing the first and last entries on the page</dt></def></entry>
</entry_list>"

最佳答案

想通了。在执行任何操作之前,您需要为子级建立索引。

var entry = root.getChildren("entry")[0];
var def = entry.getChild("def").getChildren("dt")[0].getValue()

这就是你想要的。用于在 Google 电子表格中定义单词的脚本。

享受,詹姆斯

关于xml - 使用 XmlService 解析时如何从字典 API 获取子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25899624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com