如何在Google Apps脚本中通过XML解析获取元素ID [英] How can I get the Element Id from XML parse in Google Apps Script

查看:55
本文介绍了如何在Google Apps脚本中通过XML解析获取元素ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 retrieves employee. So using this, you can retrieve the attribute of id.

    使用上述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 of employee and the value of field, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆