anypoint-studio - 如何从 XML 中提取所有嵌套的子节点?
问题描述
我有一个像这样嵌套的 grandChild 节点的简单 XML
<parent>
<child type="reference">
<grandChild name="aaa" action="None">
<Attribute name="xxx">1</Attribute>
<grandChild name="bbb" action="None">
<Attribute name="xxx">1</Attribute>
</grandChild>
<grandChild name="ccc" action="None">
<Attribute name="xxx">1</Attribute>
</grandChild>
</grandChild>
<grandChild name="ddd" action="None">
<Attribute name="xxx">1</Attribute>
</grandChild>
</child>
</parent>
我只想提取所有 grandChild 节点的列表。我能够在迭代每个节点时实现,这给了我一个级别的列表,但不是整个 XML。是否可以通过单个数据编织来实现?
我的简单数据编织看起来像这样
%dw 2.0
output application/json
---
payload.parent.child.*grandChild.*grandChild map {
"@name": $.@name,
"Attribute" : $.*Attribute map {
"@name" : $.@name,
"#text" : trim($)
}
}
期望的输出
[
{
"@name": "bbb",
"Attribute": [
{
"@name": "xxx",
"#text": "1"
}
]
},
{
"@name": "ccc",
"Attribute": [
{
"@name": "xxx",
"#text": "1"
}
]
},
{
"@name": "aaa",
"Attribute": [
{
"@name": "xxx",
"#text": "1"
}
]
},
{
"@name": "ddd",
"Attribute": [
{
"@name": "xxx",
"#text": "1"
}
]
}
]
解决方案
这似乎有效:
payload..*grandChild map ( ( elt, index) ->
{
"@name": elt.@name,
"Attribute": elt.*Attribute map ( (attrElt, attrIndex) ->
{ "@name": attrElt.@name,
"#text": trim(attrElt)
}
)
}
)
推荐阅读
- php - CodeIgniter 中的 SQL 查询
- c# - EF 添加实体时,也将其附加到错误的实体但具有相同的属性
- php - 来自 json 解码页面的 preg 匹配
- glm - 如何在 R 中使用 glm() 函数或贝叶斯网络对二项式数据进行等价测试?
- java - Java中相同的密码导致不同的AES密钥
- boost-build - 如何将 Jambase 文件包含到我的构建中?
- ruby - 打印按哈希键排序的哈希数组
- airflow - Airflow 1.10 - 任务之间的长时间延迟
- angular - 使用 ag-grid 进行国际化
- java - Sendbird 从频道或组中删除成员