3

I'm in a situation, where i receive a query containing XML string. I'm supposed to convert it to json.

I wrote a small CF Function, that traverses/parse through the XML and conveniently transforms it into a json. Now the problem is, the XML schema has been changed, which is forcing me to re-write the CF function to suit the new schema.

Is there a more better/generic way of converting XML into json? (using ColdFusion though!)

Matt Busche
  • 14,216
  • 5
  • 36
  • 61
novein
  • 53
  • 1
  • 5

2 Answers2

6

There is XSLTJSON.

Download the XSLT stylesheet and use it with ColdFusion's XmlTransform() function.

<cfset xmlDoc  = XmlParse(yourXmlString, true)>

<cfset params  = StructNew()>
<cfset params["any-param"] = "you wish to pass to the XSL processor">

<cfset jsonStr = XmlTransform(xmlDoc, "xml-to-json.xsl", params)>
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • thanks for the response. I'm currently using cold fusion 8, I'm getting following error (I've used the XSLT v2.0) :( – novein May 24 '11 at 08:48
  • `javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: "as" attribute is not allowed on the xsl:param element! A Transformer object cannot be created that satisfies the configuration requested. This could be due to a failure in compiling the XSL text. javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: "as" attribute is not allowed on the xsl:param element! ` – novein May 24 '11 at 08:49
  • @novein: Oh, it seems that the XSLT is version 2.0, and ColdFusion (natively) only supports XSLT 1.0. I must admit that I hadn't checked for that. :-\ Sorry. – Tomalak May 24 '11 at 17:03
  • @novein: http://code.google.com/p/xml2json-xslt/ has an XSLT 1.0 stylesheet you can try. – Tomalak May 24 '11 at 18:16
  • The function is actually called `XMLTransform` not `XSLTransform`. – Seybsen Jun 11 '12 at 15:17
  • @Seybsen Whoops, you're absolutely right. Thanks for pointing this out! (It was correct in the text, and wrong in the code. D'oh.) – Tomalak Jun 11 '12 at 15:24
1

Got this working today, had to import the current Saxon libs and write a small java helper file.

public static String transformXML(String xmlData, String xslFile) throws SaxonApiException 
{

    StringWriter sw = new StringWriter();
    XdmNode source = null;

    Processor proc = new Processor(false);
    XsltCompiler comp = proc.newXsltCompiler();
    XsltExecutable exp = comp.compile(new StreamSource(new File(xslFile)));

    try
    {
        source = proc.newDocumentBuilder().build(new StreamSource(new ByteArrayInputStream(xmlData.getBytes("UTF-8"))));
    }
    catch (UnsupportedEncodingException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Serializer out = proc.newSerializer(sw);
    //out.setOutputProperty(Serializer.Property.METHOD, "html");
    out.setOutputProperty(Serializer.Property.INDENT, "yes");
    XsltTransformer trans = exp.load();
    trans.setInitialContextNode(source);
    trans.setDestination(out);
    trans.transform();

    return sw.toString();
}