一个json转xml的例子

xml的<>里面很多字符都显示为非法,所以就转了下。json用的是jsoncpp开源库

int CCMPUtility::jsonToXmlString(Json::Value value,std::string &strXml)
{
if(0 == value.size()) return -1;
std::vector<std::string> vecKeys = value.getMemberNames();
for(std::vector<std::string>::iterator it = vecKeys.begin();it != vecKeys.end();it++)
{
std::string strKey = *it;
//const char *szNum = strKey.c_str();
std::string strKeyNoSpace;
//if(isdigit(*szNum))
strKeyNoSpace = "prefix_"+strKey;/*XML tag cannot begin with a number*/
//else
// strKeyNoSpace = strKey;
replaceString(strKeyNoSpace," ","_");/*XML tag cannot include space*/
replaceString(strKeyNoSpace,"^","");/*XML tag cannot include space*/
if(value[strKey.c_str()].isArray())
{
Json::Value array = value[strKey.c_str()];
std::string strArray="";
strXml += "<"+strKeyNoSpace+">\n";
for(Json::Value::iterator itV = array.begin();itV != array.end();itV++)
{
std::string str;
if((*itV).isString())
{
str = (*itV).asString();
}
else if((*itV).isInt())
{
int nItem = (*itV).asInt();
std::ostringstream stream;
stream<<nItem;
str = stream.str();
}
if(!str.empty())
strXml += "<li>"+str+"</li>\n";
}
strXml += "</"+strKeyNoSpace+">";
}
else if(value[strKey.c_str()].isObject())
{
Json::Value ItemValue = value[strKey.c_str()];
strXml += "<"+strKeyNoSpace+">";
jsonToXmlString(ItemValue,strXml);
strXml += "</"+strKeyNoSpace+">";
}
else if(value[strKey.c_str()].isString())
{
std::string strItem = value[strKey.c_str()].asString();
if(!strItem.empty())
strXml  += "<"+strKeyNoSpace+">"+strItem+"</"+strKeyNoSpace+">\n";
}
else if(value[strKey.c_str()].isInt())
{
std::ostringstream stream;
stream << value[strKey.c_str()].asInt();
std::string strItem = stream.str();
if(!strItem.empty())
strXml  += "<"+strKeyNoSpace+">"+strItem+"</"+strKeyNoSpace+">\n";
}

}
return 0;
}
Groovy 内置了 `JsonSlurper` 和 `MarkupBuilder` 类可以方便地进行 JSONXML换。 下面是一个JSON 换为 XML例子: ```groovy import groovy.json.JsonSlurper import groovy.xml.MarkupBuilder def json = '{"name": "John", "age": 30, "city": "New York"}' def slurper = new JsonSlurper() def object = slurper.parseText(json) def writer = new StringWriter() def builder = new MarkupBuilder(writer) builder.'person' { 'name'(object.name) 'age'(object.age) 'city'(object.city) } def xml = writer.toString() println xml ``` 输出: ```xml <person> <name>John</name> <age>30</age> <city>New York</city> </person> ``` 其中,`JsonSlurper` 类用于解析 JSON 字符串为 Groovy 的对象,`MarkupBuilder` 类用于构建 XML 文档。在 `MarkupBuilder` 中,使用 `'person'` 定义了父节点,然后使用 `'name'`、`'age'` 和 `'city'` 分别作为子节点,使用 `object` 中的属性作为文本内容。 如果你想要将 XML 换为 JSON,可以使用 `JsonOutput` 类的 `toJson` 方法将 Groovy 对象换为 JSON 字符串,示例代码如下: ```groovy import groovy.json.JsonOutput import groovy.xml.XmlUtil def xml = '<person><name>John</name><age>30</age><city>New York</city></person>' def root = XmlUtil.parseText(xml) def object = [:].withDefault { k -> root."$k"*.text().join('') } def json = JsonOutput.toJson(object) println json ``` 输出: ```json {"name":"John","age":30,"city":"New York"} ``` 在这里,`XmlUtil` 类用于将 XML 字符串解析为 DOM 对象,然后使用 `withDefault` 方法将 DOM 对象换为 Groovy 的 `Map` 对象。最后使用 `JsonOutput` 类的 `toJson` 方法将 `Map` 对象换为 JSON 字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值