是否可以在 ColdFusion Fusion 8 中将 XML 字符串转换为 JSON

发布于 2024-11-09 09:01:20 字数 190 浏览 0 评论 0原文

我遇到的情况是,我收到一个包含 XML 字符串的查询。我应该将其转换为 json。

我编写了一个小型 CF 函数,它遍历/解析 XML 并方便地将其转换为 json。现在的问题是,XML 模式已更改,这迫使我重新编写 CF 函数以适应新模式。

有没有更好/通用的方法将 XML 转换为 json? (不过使用 ColdFusion!)

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!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2024-11-16 09:01:20

XSLTJSON

下载 XSLT 样式表并将其与 ColdFusion 的 XmlTransform() 一起使用 函数

<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)>

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)>
拥抱影子 2024-11-16 09:01:20

今天开始工作,必须导入当前的 Saxon 库并编写一个小的 java 帮助程序文件。

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();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文