Forum Discussion
@darshthakkar you can use the Apace Velocity support that is available within the Generator Snaps, for example
for the following input JSON (e.g. JSON Generator) that:
- includes two fields (
msg1
,msg2
) that are intended to be combined into one output XML element - a
vary
field that could be eithernull
, contain a single String value, or a list of string values
[
{
"msg1" : "Hello", "msg2":"World", "num" : 1, "vary": null
},
{
"msg1" : "Hi", "msg2":"World", "num" : 2, "vary": "b"
},
{
"msg1" : "Hey", "msg2":"World", "num" : 3, "vary": ["b", "c"]
}
]
An XML Generator configuration like:
“Use default value for substitution” is enabled if you wish to replace any null
values with an empty String equivalent (see “Default value for substitution”)
The “Edit XML” contains:
#set( $msg = $msg1 + " " + $msg2 )
<root>
<msg>$msg</msg>
<num>$num</num>
#if ($vary.getClass().getName() == "java.util.ArrayList")
#foreach( $v in $vary )
<vary>$v</vary>
#end
#elseif ($vary.getClass().getName() == "java.lang.String")
<vary>$vary</vary>
#end
</root>
This will result in the following output:
Thank you @robin, your help is much appreciated.
- darshthakkar4 years agoValued Contributor
@robin, would it be safe to assume that this will work for generating html files from XML generator snap?
- darshthakkar4 years agoValued Contributor
Thanks @robin, I haven’t had a chance to test the solution you provided but I’m sure it would have worked.
I went with a different route for my development so the concern I was having has been resolved.XML Generator does intake dynamic values if they have been binded properly prior to it (using Aggregate or Group By), the only challenge I found was ingesting native html tags because
<a href ="$URL">
wasn’t generating the desired output as the tags gets converted to snapLogic’s predefined symbols like< = <
,so on and so forth. Check these links for detailed explanation on tags conversion:What I found and worked for me was defining the html format (if you’re trying to generate html files; could be any format you’re interested into) in the mapper itself/file writer and then let the format do its job.
Thanking @robin for his suggestions, time and help on this. Signing off from this thread.