Boucle sur un élément XML
Bonjour,
Je débute avec Groovy, j'aimerais créer un fichier XML à partir d'un existant, mais j'ai un problème dans ma boucle, je n'obtiens pas le résultat souhaité
input xml:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?xml version="1.0" encoding="UTF-8" ?>
<root>
<id>0035964572</id>
<issue_date>20220214</issue_date>
<request_quote_id>101974792</request_quote_id>
<request_quote_line_item_id>000010</request_quote_line_item_id>
<expiration_date>20220630</expiration_date>
<sold_to_party_id>77288060</sold_to_party_id>
<attachments>
<file_name>file1</file_name>
<title>title1</title>
<type>type1</type>
<mime_type>ùome1</mime_type>
<location>location1</location>
</attachments>
<attachments>
<file_name>file2</file_name>
<title>title2</title>
<type>type2</type>
<mime_type>mime2</mime_type>
<location>location2</location>
</attachments>
</root> |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| def Message processData(Message message) {
def writer = new StringWriter()
def builder = new MarkupBuilder(writer)
Reader reader = message.getBody(Reader);
def fichierXML = new XmlSlurper().parse(reader);
builder.Root {
RequestQuoteHeader {
'ListOfAttachment' {
fichierXML.attachments.each {
'Attachment' {
'text_id'('FNAM')
'file_name'(fichierXML.attachments.file_name)
'title'(fichierXML.attachments.title)
'mim_type'(fichierXML.attachments.mim_type)
'location'(fichierXML.attachments.location)
}
}
}
}
}
message.setBody(writer.toString())
return message;
} |
Résultat : j'ai ces mauvaises concaténations :
Citation:
title1title2 location1location2
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <Root>
<RequestQuoteHeader>
<ListOfAttachment>
<Attachment>
<text_id>FNAM</text_id>
<file_name>file1file2</file_name>
<title>title1title2</title>
<mim_type></mim_type>
<location>location1location2</location>
</Attachment>
<Attachment>
<text_id>FNAM</text_id>
<file_name>file1file2</file_name>
<title>title1title2</title>
<mim_type></mim_type>
<location>location1location2</location>
</Attachment>
</ListOfAttachment>
</RequestQuoteHeader>
</Root> |
Auriez-vous des suggestions pour éviter ces concaténations ?
Cordialement.