有没有办法通过在soapUI上使用groovy脚本将Json文件转换为XML? [英] Is there a way to convert Json file to XML by using groovy script on soapUI?

查看:201
本文介绍了有没有办法通过在soapUI上使用groovy脚本将Json文件转换为XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将Json文件格式转换为XML格式,我已经看到很多甚至在java中使用它的网页我可以看到有很多人能够做到这一点,但我找不到

I need to convert Json file format into XML format, I've seen a lot of webpages that use this even in java I can see there are a lot of people who are capable of doing it but I can't find a way on groovy.

I have a file like this one:
{
"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
}
} 

我希望能够转换直接转换为XML,因为我们需要使用它来动态创建操作请求。

And I'd like to be able to convert directly to XML, since we need to use that to dynamically create the request for an operation.

预先感谢您们

推荐答案

她是链接 a>可以帮助你。

Her is the link that can help you.

基于上面的链接,这里是完整的groovy脚本。

Based on the above link, here is the complete groovy script for the same.

import net.sf.json.JSON
import net.sf.json.JSONSerializer
import net.sf.json.xml.XMLSerializer

String str = '''{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } }'''
JSON json = JSONSerializer.toJSON( str )
XMLSerializer xmlSerializer = new XMLSerializer()
xmlSerializer.setTypeHintsCompatibility( false )
String xml = xmlSerializer.write( json )
System.out.println(xml)

顺便说一下,您需要下载 library 并将其添加到 classpath 中,在这种情况下,将其复制到SOAPUI_HOME / bin / ext下目录。

By the way, you need to download the library and add it to the classpath, in this case copy it under SOAPUI_HOME/bin/ext directory.

这篇关于有没有办法通过在soapUI上使用groovy脚本将Json文件转换为XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆