英文:
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 = '<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);
}
-
When this script is run, the following result is obtained.
[ {"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"} ]
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论