使用jsonlib从JSON转换为XML时的无效字符 [英] Invalid character while converting from JSON to XML using jsonlib

查看:263
本文介绍了使用jsonlib从JSON转换为XML时的无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java中的jsonlib将JSON字符串转换为XML.

I'm trying to convert a JSON string to XML using jsonlib in Java.

    JSONObject json = JSONObject.fromObject(jsonString); 
    XMLSerializer serializer = new XMLSerializer();
    String xml = serializer.write( json );  
    System.out.println(xml);

我得到的错误是

    nu.xom.IllegalNameException: 0x24 is not a legal NCName character

这里的问题是我的JSON中有一些无效XML字符的属性.例如.我有一个名为"$ t"的属性.尝试使用该名称创建XML标记时,XMLSerializer会引发异常,因为XML标记名称中不允许$.有什么方法可以覆盖由序列化程序完成的XML格式检查吗?

The problem here is that I have some properties in my JSON that are invalid XML characters. eg. I have a property named "$t". The XMLSerializer throws the exception while trying to create a XML tag in this name because $ is not allowed in XML tag names. Is there any way in which I can override this XML well formedness check done by the serializer?

推荐答案

您可以遍历json.keySet(必要时递归),并用有效的密钥替换无效的密钥(使用removeadd).

You could loop over json.keySet (recursively if necessary) and replace invalid keys with valid ones (using remove and add).

这篇关于使用jsonlib从JSON转换为XML时的无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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