解析XML – 获取每个值的属性 – Google App Script

huangapple go评论69阅读模式
英文:

Parse XML - Get attribute each value - Google App Script

问题

以下是您要的翻译内容:

// 以下的parseXML代码返回所有子值,我想将key属性设置为每个值。

代码

function myFunction() {

  // 这是示例XML数据
  var response = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><h:TrackingId xmlns:h="https://ntdbdt.com/ntd/v13">56453-45634-4634-4563-456</h:TrackingId></s:Header><s:Body><GetAccountsInfoResponse xmlns="https://fhnbdf.com/Customer/v13"><AccountsInfo xmlns:a="https://ndhnb.com/nf/v13/Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:AccountInfo><a:Id>654547</a:Id><a:Name>Test Outlet</a:Name><a:Number>F777TBZM</a:Number><a:AccountLifeCycleStatus>Active</a:AccountLifeCycleStatus><a:PauseReason i:nil="true"/></a:AccountInfo><a:AccountInfo><a:Id>6784576</a:Id><a:Name>Test Workshop</a:Name><a:Number>X6667</a:Number><a:AccountLifeCycleStatus>Active</a:AccountLifeCycleStatus><a:PauseReason i:nil="true"/></a:AccountInfo><a:AccountInfo><a:Id>64563</a:Id><a:Name>Test Shop</a:Name><a:Number>R666U96Q</a:Number><a:AccountLifeCycleStatus>Pause</a:AccountLifeCycleStatus><a:PauseReason>2</a:PauseReason></a:AccountInfo></AccountsInfo></GetAccountsInfoResponse></s:Body></s:Envelope>';
  
  try {
    var document = XmlService.parse(response);
    var root = document.getRootElement();
    var atom = XmlService.getNamespace('http://schemas.xmlsoap.org/soap/envelope/');
    var entries = document.getRootElement().getChildren('Body', atom);

    if (entries) {
      _.each(entries[0].getChildren(), function(child) {
        accounts_list = child.getValue();
      });
    }
  } catch(err) {
    Logger.log(err);
    throw new Error();
  }
  Logger.log([accounts_list])
  return accounts_list;
}

响应

[654547Test OutletF777TBZMActive6784576Test WorkshopX6667Active64563Test ShopR666U96QPause2]

我想要获取这个响应

[
{id=654547, name=Test Outlet, number=F777TBZM, accountLifeCycleStatus=Active, pauseReason=}, 
{id=6784576, name=Test Workshop, number=X6667, accountLifeCycleStatus=Active, pauseReason=}, 
{id=64563, name=Test Shop, number=R666U96Q, accountLifeCycleStatus=Pause, pauseReason=2}, 
]

希望这有所帮助!

英文:

The parseXML code below return all the child values, I would like to set the key attribute to each value.

Code:

    function myFunction() {
// This is the sample XML data
var response = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><h:TrackingId xmlns:h="https://ntdbdt.com/ntd/v13">56453-45634-4634-4563-456</h:TrackingId></s:Header><s:Body><GetAccountsInfoResponse xmlns="https://fhnbdf.com/Customer/v13"><AccountsInfo xmlns:a="https://ndhnb.com/nf/v13/Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:AccountInfo><a:Id>654547</a:Id><a:Name>Test Outlet</a:Name><a:Number>F777TBZM</a:Number><a:AccountLifeCycleStatus>Active</a:AccountLifeCycleStatus><a:PauseReason i:nil="true"/></a:AccountInfo><a:AccountInfo><a:Id>6784576</a:Id><a:Name>Test Workshop</a:Name><a:Number>X6667</a:Number><a:AccountLifeCycleStatus>Active</a:AccountLifeCycleStatus><a:PauseReason i:nil="true"/></a:AccountInfo><a:AccountInfo><a:Id>64563</a:Id><a:Name>Test Shop</a:Name><a:Number>R666U96Q</a:Number><a:AccountLifeCycleStatus>Pause</a:AccountLifeCycleStatus><a:PauseReason>2</a:PauseReason></a:AccountInfo></AccountsInfo></GetAccountsInfoResponse></s:Body></s:Envelope>';
try {
var document = XmlService.parse(response);
var root = document.getRootElement();
var atom = XmlService.getNamespace('http://schemas.xmlsoap.org/soap/envelope/');
var entries = document.getRootElement().getChildren('Body', atom);
if (entries) {
_.each(entries[0].getChildren(), function(child) {
accounts_list = child.getValue();
});
}
} catch(err) {
Logger.log(err);
throw new Error();
}
Logger.log([accounts_list])
return accounts_list;
}

response:

[654547Test OutletF777TBZMActive6784576Test WorkshopX6667Active64563Test ShopR666U96QPause2]

I would like to get this response:

[
{id=654547, name=Test Outlet, number=F777TBZM, accountLifeCycleStatus=Active, pauseReason=}, 
{id=6784576, name=Test Workshop, number=X6667, accountLifeCycleStatus=Active, pauseReason=}, 
{id=64563, name=Test Shop, number=R666U96Q, accountLifeCycleStatus=Pause, pauseReason=2}, 
]

Any help?

答案1

得分: 2

修改后的脚本:

function myFunction() {
  var response = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><h:TrackingId xmlns:h="https://ntdbdt.com/ntd/v13">56453-45634-4634-4563-456</h:TrackingId></s:Header><s:Body><GetAccountsInfoResponse xmlns="https://fhnbdf.com/Customer/v13"><AccountsInfo xmlns:a="https://ndhnb.com/nf/v13/Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:AccountInfo><a:Id>654547</a:Id><a:Name>Test Outlet</a:Name><a:Number>F777TBZM</a:Number><a:AccountLifeCycleStatus>Active</a:AccountLifeCycleStatus><a:PauseReason i:nil="true"/></a:AccountInfo><a:AccountInfo><a:Id>6784576</a:Id><a:Name>Test Workshop</a:Name><a:Number>X6667</a:Number><a:AccountLifeCycleStatus>Active</a:AccountLifeCycleStatus><a:PauseReason i:nil="true"/></a:AccountInfo><a:AccountInfo><a:Id>64563</a:Id><a:Name>Test Shop</a:Name><a:Number>R666U96Q</a:Number><a:AccountLifeCycleStatus>Pause</a:AccountLifeCycleStatus><a:PauseReason>2</a:PauseReason></a:AccountInfo></AccountsInfo></GetAccountsInfoResponse></s:Body></s:Envelope>';

  var document = XmlService.parse(response);
  var root = document.getRootElement();
  var ns1 = root.getNamespace();
  var ns2 = XmlService.getNamespace("https://fhnbdf.com/Customer/v13");
  var accountInfo = root.getChild("Body", ns1).getChild("GetAccountsInfoResponse", ns2).getChild("AccountsInfo", ns2).getChildren();
  var accounts_list = accountInfo.map(e => e.getChildren().reduce((o, f) => (o[f.getName()] = f.getValue(), o), {}));
  console.log(accounts_list);
}
  • 运行此脚本时,将获得以下结果。

    [
      {"Id":"654547","Name":"Test Outlet","Number":"F777TBZM","AccountLifeCycleStatus":"Active","PauseReason":""},
      {"Id":"6784576","Name":"Test Workshop","Number":"X6667","AccountLifeCycleStatus":"Active","PauseReason":""},
      {"Id":"64563","Name":"Test Shop","Number":"R666U96Q","AccountLifeCycleStatus":"Pause","PauseReason":"2"}
    ]
    

参考资料:

英文:

In your script, how about the following modification?

Modified script:

function myFunction() {
  var response = &#39;&lt;s:Envelope xmlns:s=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;&lt;s:Header&gt;&lt;h:TrackingId xmlns:h=&quot;https://ntdbdt.com/ntd/v13&quot;&gt;56453-45634-4634-4563-456&lt;/h:TrackingId&gt;&lt;/s:Header&gt;&lt;s:Body&gt;&lt;GetAccountsInfoResponse xmlns=&quot;https://fhnbdf.com/Customer/v13&quot;&gt;&lt;AccountsInfo xmlns:a=&quot;https://ndhnb.com/nf/v13/Entities&quot; xmlns:i=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;&lt;a:AccountInfo&gt;&lt;a:Id&gt;654547&lt;/a:Id&gt;&lt;a:Name&gt;Test Outlet&lt;/a:Name&gt;&lt;a:Number&gt;F777TBZM&lt;/a:Number&gt;&lt;a:AccountLifeCycleStatus&gt;Active&lt;/a:AccountLifeCycleStatus&gt;&lt;a:PauseReason i:nil=&quot;true&quot;/&gt;&lt;/a:AccountInfo&gt;&lt;a:AccountInfo&gt;&lt;a:Id&gt;6784576&lt;/a:Id&gt;&lt;a:Name&gt;Test Workshop&lt;/a:Name&gt;&lt;a:Number&gt;X6667&lt;/a:Number&gt;&lt;a:AccountLifeCycleStatus&gt;Active&lt;/a:AccountLifeCycleStatus&gt;&lt;a:PauseReason i:nil=&quot;true&quot;/&gt;&lt;/a:AccountInfo&gt;&lt;a:AccountInfo&gt;&lt;a:Id&gt;64563&lt;/a:Id&gt;&lt;a:Name&gt;Test Shop&lt;/a:Name&gt;&lt;a:Number&gt;R666U96Q&lt;/a:Number&gt;&lt;a:AccountLifeCycleStatus&gt;Pause&lt;/a:AccountLifeCycleStatus&gt;&lt;a:PauseReason&gt;2&lt;/a:PauseReason&gt;&lt;/a:AccountInfo&gt;&lt;/AccountsInfo&gt;&lt;/GetAccountsInfoResponse&gt;&lt;/s:Body&gt;&lt;/s:Envelope&gt;&#39;;

  var document = XmlService.parse(response);
  var root = document.getRootElement();
  var ns1 = root.getNamespace();
  var ns2 = XmlService.getNamespace(&quot;https://fhnbdf.com/Customer/v13&quot;);
  var accountInfo = root.getChild(&quot;Body&quot;, ns1).getChild(&quot;GetAccountsInfoResponse&quot;, ns2).getChild(&quot;AccountsInfo&quot;, ns2).getChildren();
  var accounts_list = accountInfo.map(e =&gt; e.getChildren().reduce((o, f) =&gt; (o[f.getName()] = f.getValue(), o), {}));
  console.log(accounts_list);
}
  • When this script is run, the following result is obtained.

    [
      {&quot;Id&quot;:&quot;654547&quot;,&quot;Name&quot;:&quot;Test Outlet&quot;,&quot;Number&quot;:&quot;F777TBZM&quot;,&quot;AccountLifeCycleStatus&quot;:&quot;Active&quot;,&quot;PauseReason&quot;:&quot;&quot;},
      {&quot;Id&quot;:&quot;6784576&quot;,&quot;Name&quot;:&quot;Test Workshop&quot;,&quot;Number&quot;:&quot;X6667&quot;,&quot;AccountLifeCycleStatus&quot;:&quot;Active&quot;,&quot;PauseReason&quot;:&quot;&quot;},
      {&quot;Id&quot;:&quot;64563&quot;,&quot;Name&quot;:&quot;Test Shop&quot;,&quot;Number&quot;:&quot;R666U96Q&quot;,&quot;AccountLifeCycleStatus&quot;:&quot;Pause&quot;,&quot;PauseReason&quot;:&quot;2&quot;}
    ]
    

References:

huangapple
  • 本文由 发表于 2023年6月16日 01:33:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定