Joe Dickson
12/12/2022, 5:28 PMsknowlton
12/12/2022, 6:10 PMsknowlton
12/12/2022, 6:11 PM/**
* @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;
}bdw429s
12/12/2022, 6:52 PM