xml - 如何在 GAS 中将 XML 路径拆分为数组
问题描述
搜索将 XML 路径拆分为数组的好方法。我觉得我的解决方案并不像我想要的那样可靠。
我有的:<product><containeditem><productidentifier>
我想要得到的是一个数组,如:[product, containeditem, productidentifier]
我的代码:
function GetPathArray(path) {
if (path != null) {
path = path.substring(0, path.length - 1);
path = path.substring(1);
var pathArray = [{}];
pathArray = path.split("><");
return pathArray;
}
else {
return null;
}
}
解决方案
为确保返回数组而不是字符串,您可以将其用于问题中的简单情况:
var path = '<product><containeditem><productidentifier>';
console.log( getPathArray(path) );
function getPathArray(path){
return path.slice(1, -1).split('><');
}
该slice
函数丢弃第一个和最后一个字符(开始和结束<
和>
)。
那么这split
就是你所需要的 - 因为它返回一个数组。
对于更复杂的字符串,这几乎肯定是不够的。
推荐阅读
- json - Groovy 收集嵌套元素和外部元素
- amazon-web-services - 无法将 EFS 附加到 EC2。我们尝试了各种挂载方式,即使它抛出相同的错误
- python - Qweb:如何使用索引计算数组的值?
- java - Java:具有所有依赖项的瘦 jar
- javascript - 在 IIFE 中两次声明一个变量
- arrays - 如何从字典数组中检索具有相同名称的所有键的值
- javascript - 我在使用语义 ui 反应下拉菜单时得到了“未捕获的类型错误:_this.ref.querySelector 不是函数”
- java - Java Swing Graphics2d 旋转跳跃
- mysql - 如何在 Node JS 中处理 MySQL 连接的正确方法
- vba - 每次出现表单时(隐藏而不是关闭之后)如何运行代码?