XML StAX Processing with Coldfusion
XML, ColdfusionSeveral problems with Coldfusion XML processing and solution set:
- Slow: due to DOM processor (Xerces)
- Inconsistent Round-Tripping (i.e. serialization / deserialization) :
- (un-) escaping characters
- non-printable characters
- missing / multiple XML declaration(s)
- character set problems
- et al.
- Kludgy Tree API yes you can use structure and array functions, just don't like it.
Note, don't see this as a flame on Adobe/Macromedia. The Coldfusion XML implementation uses best available technology at the time and APIs that fit in with the language paradigm. Just have better options now.
Slow Solution
Woodstox: Jim Collins wrote a CFDJ article about using a StAX XML processor with Coldfusion. Unfortunately, as near as I can tell, his open source project to integrate Coldfusion with Woodstox was never released. Testing demonstrates a StAX processor, such as Woodstox, provides very efficient (read: fast) XML processing, and a more intuitive API, than a SAX processor. But still not intuitive enough for me. So enter...
Round-Tripping & API Solutions
XOM provides a very easy to use API that roundtrips XML like a champ. Correct I/O is XOM's middle name. But it doesn't integrate natively with a StAX processor unless you use...
NUX is like a Swiss Army knife for XML. But the purpose of this entry just know it's the glue between XOM and Woodstox (note that NUX includes the XOM jar so you do not need to download it separately).
Code
Download the Woodstox and NUX jars and configure them in your Coldfusion classpath. The examples below build upon each other.
Setup
Creates a XOM Builder object backed by a StAX processor.
inputFactory = createObject("java", "com.ctc.wstx.stax.WstxInputFactory").init()
builder = createObject("java", "nux.xom.io.StaxUtil").createBuilder(local.joInputFactory, javacast("null", "")
XMLParse() Alternative
Returns a XOM Document object. Note: provide XMLString variable.
document = builder.create(XMLString)
XMLSearch() Alternative
Returns a XOM Nodes object (i.e. an iterator of Node objects). Note: provide XQueryString variable.
nodes = createObject("java", "nux.com.xquery.XQueryUtil").xquery(document, XQueryString)
loop from="0" to="#nodes.size() - 1#" index="idx"
element = nodes.get(javacast("int", idx))
attributeValue = element.getAttribute("attributeName").getValue()
attributeText = element.getValue
/loop
ToString() Alternative
Creates a string representation of the XML.
XMLString = document.toXML()




Loading....