mauzer
10/25/2023, 9:40 AMAdam Cameron
Adam Cameron
sknowlton
10/25/2023, 10:48 AMsknowlton
10/25/2023, 10:48 AM/**
* @param input Data to convert into XML. (Required)
* @param element Used to name the root element. (Required)
* @param attributeMeta Checks for the presence of a key in attributeMeta when rendering the key in input, and writes out the contents as attribute/value pairs for the corresponding key
* @return Returns a string.
* @author Phil Arnold (<mailto:philip.r.j.arnold@googlemail.com|philip.r.j.arnold@googlemail.com>) with modifications by Samuel Knowlton (<mailto:sam@inleague.io|sam@inleague.io>) - removed lcasing and added support for attributeMeta
* @version September 9, 2009 / September 29, 2021
**/
public string function toXML( required input, required string element, struct attributeMeta = {} ) {
var i = 0;
var s = createObject( "java", "java.lang.StringBuilder" ).init()
var s1 = "";
s1 = arguments.element;
if ( right( s1, 1 ) eq "s" ) {
s1 = left( s1, len( s1 ) - 1 );
}
s.append( renderXMLAttribute( arguments.element, arguments.attributeMeta ) );
if ( isArray( arguments.input ) ) {
for ( i = 1; i lte arrayLen( arguments.input ); i = i + 1 ) {
if ( isSimpleValue( arguments.input[ i ] ) ) {
s.append( renderXMLAttribute( s1, attributeMeta ) & arguments.input[ i ] & "</#s1#>" );
}
else {
s.append( toXML( arguments.input[ i ], s1 ) );
}
}
}
else if ( isStruct( arguments.input ) ) {
for ( i in arguments.input ) {
if ( isSimpleValue( arguments.input[ i ] ) ) {
s.append( renderXMLAttribute( i, attributeMeta ) & arguments.input[ i ] & "</#i#>" );
}
else {
s.append( toXML( arguments.input[ i ], i ) );
}
}
}
else {
s.append( xmlFormat( arguments.input ) );
}
s.append( "</#arguments.element#>" );
return s.toString();
}
/**
* renderXMLAttribute
* @hint A totally low-rent way to render out XML attributes (e.g. <foo bar="baz">) by checking for the presence of the key in a "sister struct" containing attribute meta nodes. Can't handle multiple nodes with the same name.
* @return String OPENING attribute only (e.g. just <foo bar="baz">)
*/
public string function renderXMLAttribute( required string key, required struct attributeMeta ) {
var attr = '<#arguments.key#';
if ( attributeMeta.keyExists( arguments.key ) ) {
for ( var metaKey in arguments.attributeMeta[ arguments.key ].keyArray() ) {
attr &= ' #metaKey#="' & arguments.attributeMeta[ arguments.key ][ metaKey ] & '"';
}
}
attr &= '>';
return attr;
}mauzer
10/25/2023, 10:49 AMAdam Cameron
Adam Cameron
mauzer
10/25/2023, 11:37 AMAdam Cameron
Adam Cameron
json = serializeJson({key="value"})
xml = xmlParse("<nowItIsXml>#encodeForXml(json)#</nowItIsXml>")
writeDump([
json = json,
xml = xml
])
But that's not useful is it? Why? Cos it doesn't have the XML schema you need, and doubtlessly also needs to think about the schema of the JSON as well.
So... right... "are there any cf/lucee functions that can convert json into xml ?" is not a well-informed question.
CFML has functions for handling JSON. It also has functions for working with XML.
But this is not going to help you transform your JSON to your XML. And no-one here can given an informed answered as to how to deal with either of those with only "are there any cf/lucee functions that can convert json into xml ?" to work with.Adam Cameron
dswitzer
10/25/2023, 12:06 PMdswitzer
10/25/2023, 12:07 PMdswitzer
10/25/2023, 12:08 PM