[英]How to add list of Nodes in existing Node Xml in groovy
I have such a XML structure, with list of 'attachments', and I want iterate incoming List<String> files
and create a groovy.util.Node for each of them and then return the builded Node.我有这样一个 XML 结构,带有“附件”列表,我想迭代传入的
List<String> files
并为每个文件创建一个 groovy.util.Node,然后返回构建的节点。 In code below I use only the fist element from list(base64 file), but I want to create attachments
dynamically based on array size.在下面的代码中,我仅使用列表(base64 文件)中的第一个元素,但我想根据数组大小动态创建
attachments
。 I am new in groovy and can't find the way how to expand the Node properly.我是 groovy 的新手,找不到正确扩展节点的方法。
import groovy.abi.XML
class TestService {
Node buildNode(List<String> files) {
Node node = XML.builder().
"sab:sendExternalEmail"("xmlns:sab": "http://sab/") {
"sab:to"('client@gmail.com')
"sab:subject"('Reply')
"sab:body"('Body')
"sab:from"('sender@gmail.com')
"sab:attachments"() {
"sab:attachment"() {
"sab:fileName"('file1')
"sab:fileBase64"(files[0])
}
}
}
}
}
just to gibe you an idea how to work with builders:只是为了让您了解如何与建设者合作:
import groovy.util.NodeBuilder
import groovy.xml.XmlUtil
def attachments = [
"name 1": "content 1",
"name 2.txt": "content 2"
]
def xml = new NodeBuilder().a{
for(a in attachments){
attacchment{
fileName(a.key)
fileBase64(a.value)
}
}
}
println XmlUtil.serialize(xml)
result:结果:
<?xml version="1.0" encoding="UTF-8"?><a>
<attacchment>
<fileName>name 1</fileName>
<fileBase64>content 1</fileBase64>
</attacchment>
<attacchment>
<fileName>name 2.txt</fileName>
<fileBase64>content 2</fileBase64>
</attacchment>
</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.