Created
December 2, 2010 18:00
-
-
Save aviflax/725761 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name="prettyXml"> | |
<cfargument name="xml" required="yes" type="string" /> | |
<!--- initialize sax builder ---> | |
<cfset builder = createObject("java", "org.jdom.input.SAXBuilder").init()> | |
<!--- initialize the formatter ---> | |
<cfset format = createObject("java", "org.jdom.output.Format").getPrettyFormat()> | |
<!--- initialize the outputer ---> | |
<cfset out = createObject("java", "org.jdom.output.XMLOutputter").init(format)> | |
<!--- even though we've already converted to a string, it's not stringy enough for Java until we do this ---> | |
<cfset xmlstring = createObject("java", "java.io.StringReader").init(xml)> | |
<cfset document = builder.build(xmlstring)> | |
<cfset prettyxml = out.outputString(document)> | |
<cfreturn prettyxml> | |
</cffunction> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name="prettyXml"> | |
<cfargument name="xml" required="yes" type="any" /> | |
<cfscript> | |
if (isSimpleValue(xml)) { | |
// A string was sent in, we need to parse it into a JDOM Node before outputting it | |
// initialize sax builder | |
builder = createObject("java", "org.jdom.input.SAXBuilder").init(); | |
// even though we've already converted to a string, it's not stringy enough for Java until we do this | |
xmlstring = createObject("java", "java.io.StringReader").init(xml); | |
// convert | |
node = builder.build(xmlstring); | |
} else { | |
/* A ColdFusion XML object was sent in, which can be converted into a JDOM Node | |
This is faster than parsing a string */ | |
// initialize DOM builder | |
builder = createObject("java", "org.jdom.input.DOMBuilder").init(); | |
// convert | |
node = builder.build(xml); | |
} | |
// initialize the formatter | |
format = createObject("java", "org.jdom.output.Format").getPrettyFormat(); | |
// initialize the outputter | |
out = createObject("java", "org.jdom.output.XMLOutputter").init(format); | |
// output the node as a pretty-printed string | |
prettyxml = out.outputString(node); | |
return prettyxml; | |
</cfscript> | |
</cffunction> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jdom.input.SAXBuilder | |
import org.jdom.output.Format | |
import org.jdom.output.XMLOutputter | |
docString = '<foo><message>happy hanukkah!</message></foo>' | |
doc = new SAXBuilder().build(new StringReader(docString)) | |
outputter = new XMLOutputter(Format.getPrettyFormat()) | |
docString = outputter.outputString(doc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// assumes we already have a builder and the new Form element XML in a variable named newFormString | |
new_form_doc = builder.build(newFormString) | |
new_form_element = new_form_doc.getRootElement() | |
// assumes session.product is a JDOM document. If it isn't, then convert it. | |
product_root = session.product.getRootElement() | |
old_form_element = product_root.getChild("InputForm") | |
old_form_element_index = product_root.indexOf(old_form_element) | |
product_root.removeChild("InputForm") | |
product_root.addContent(old_form_element_index, new_form_element.detach()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment