如何在Google Apps脚本中通过XML解析获取元素ID [英] How can I get the Element Id from XML parse in Google Apps Script
问题描述
Xml代码
<directory>
<fieldset>
<field id="displayName">Display name</field>
</fieldset>
<employees>
<employee id="123">
<field id="displayName">John Doe</field>
</employee>
<employee id="160">
<field id="displayName">Jane Doe</field>
</employee>
我可以使用XmlService.parse解析信息并获取每个元素中的值,但无法获取员工/元素ID号
I can parse the information with XmlService.parse and get the value within each element, but I cannot get the employee/element id number
运行此命令时:
var roots = document.getRootElement().getChild('employees').getChildren();
我得到了单个元素,但是我却无法获得ID's
I get the individual elements but I cannot get the id's
[[Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>], [Element: <employee/>]]
推荐答案
-
您要从以下xml数据中检索
123
和160
.<directory> <fieldset> <field id="displayName">Display name</field> </fieldset> <employees> <employee id="123"> <field id="displayName">John Doe</field> </employee> <employee id="160"> <field id="displayName">Jane Doe</field> </employee>
-
您要使用Google Apps脚本的XmlService实现此目的.
You want to achieve this using XmlService of Google Apps Script.
- 在您的情况下,
roots
会检索employee
.因此,使用此代码,您可以检索id
的属性.
- In your case,
roots
retrievesemployee
. So using this, you can retrieve the attribute ofid
.
使用上述xml数据时,示例脚本如下.在此示例中,将
</employees></directory>
添加到示例xml数据中.When above xml data is used, the sample script is as follows. In this sample,
</employees></directory>
is added to the sample xml data.function myFunction() { const xml = `<directory> <fieldset> <field id="displayName">Display name</field> </fieldset> <employees> <employee id="123"> <field id="displayName">John Doe</field> </employee> <employee id="160"> <field id="displayName">Jane Doe</field> </employee> </employees> </directory>`; var document = XmlService.parse(xml); var roots = document.getRootElement().getChild('employees').getChildren(); var ids = roots.map(e => e.getAttribute("id").getValue()); console.log(ids); // [123, 160] }
注意:
-
在这种情况下,请将该脚本与V8配合使用.
Note:
In this case, please use the script with V8.
如果要同时检索
employee
的id
和field
的值,则还可以使用以下脚本.If you want to retrieve both
id
ofemployee
and the value offield
, you can also use the following script.var ids = roots.map(e => ({id: e.getAttribute("id").getValue(), name: e.getChild("field").getText()}));
- 在这种情况下,检索
[{id:'123',名称:'John Doe'},{id:'160',名称:'Jane Doe'}]
.>li>当上述xml示例数据的结构与您的实际数据不同时,修改后的脚本可能无法正常工作.所以请注意这一点.
这篇关于如何在Google Apps脚本中通过XML解析获取元素ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 在这种情况下,检索
- 在您的情况下,