在使用JSon-Lib库进行XML2JSon的转换时,在JUnit测试时没有什么问题,但是在Tomcat里面跑的时候,抛出了下面的异常,查找了google,发现关于这方便的文章比较少,即使有,也需要翻墙去查找,于是就自己记录下来,以便后面的人查找翻遍。

net.sf.json.JSONException: nu.xom.ParsingException: Element type "鍥句功娴侀" must be followed by either attribute specifications, ">" or "/>". at line 1, column 46

找到最原始的一个文章连接是下面的链接,上面说明了,在不同的环境上面,在转换中文的时候,出现了使用不同的编码集导致了问题。

http://www.codeweblog.com/transfer-element-type-must-be-followed-by-either-attribute-specifications/

知道了问题的所在,但是依然不知道如何解决。于是又搜索到了一位大侠的文章《[转]Element Type Must Be Followed By Either Attribute Specifications, “>”

http://newwhx2011.iteye.com/blog/1121201

在这片文章里面,明确说了【关于new XMLSerializer().readFromFile()在读取文件内容时,从字节流转换为字符流时并没有指定编码,此处应该是json-lib代码的Bug。】,于是查看了XMLSerializer的代码,发现确实是在转换的时候,没有指定编码,于是按照他提供的思路,进行了编码,也就是自己指定编码处理。

原来的代码:

       public static String xml2json(File xmlFile) throws Exception {
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.readFromFile(xmlFile);
return json.toString(2);
}

更改后的代码,也就是把原来XMLSerializer里面的代码copy出来,同时加了一个编码的设置,现在一切都正常了

    public static String xml2json(File xmlFile) throws Exception {

        JSON json = readFromStream(new FileInputStream(xmlFile));
return json.toString(2);
} public static JSON readFromStream(InputStream stream) throws Exception {
StringBuffer xml = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(stream,"UTF-8"));
String line = null;
while ((line = in.readLine()) != null) {
xml.append(line);
}
XMLSerializer xmlSerializer = new XMLSerializer();
return xmlSerializer.read(xml.toString());
}

XML2JSON 的【net.sf.json.JSONException: nu.xom.ParsingException must be followed by either attribute specifications, ">" or "/>"】问题解决办法的更多相关文章

  1. Json_异常_net.sf.json.JSONException: JSONObject["solution"] not found.

    net.sf.json.JSONException: JSONObject["solution"] not found. 没有这个元素造成的. 问题代码: JSONObject j ...

  2. atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy

    atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy 1. 环境:使用hibernate4跟个,,要不个哪的对象系列 ...

  3. net.sf.json.JSONException: java.lang.NoSuchMethodException

    在尝试将json对象转换为list时候出现了如下错误 Exception in thread "main" net.sf.json.JSONException: java.lang ...

  4. json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

    注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...

  5. net.sf.json.JSONException: There is a cycle in the hierarchy!

    因为项目中使用了AJAX技术,jar包为:json-lib.jar,在开发过程中遇到了一个JSON-LIB和Hibernate有关的问题: 如hibernate延迟加载错误,这都是些老问题了,一看就知 ...

  6. json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

    转:json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 执行:JSONArray arr ...

  7. net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案

    net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案 今天在用List集合转换成json数组的时候发生了这个错误,这个 ...

  8. 前后台JSON传值得一个问题和异常处理net.sf.json.JSONException: Unquotted string '"name"'

    项目中做导入的时候遇到个bug,用JSON.stringify()序列号json对象传给后台:然后后台通过getPatameter()获取值时,前台的英文引号变成了中文引号. 原来代码如下:(自己排查 ...

  9. 找了两个小时的错误,net.sf.json.JSONException: JSON keys cannot be null.

    因为数据库里面一条记录插入的是NULL,所以导致报了net.sf.json.JSONException: JSON keys cannot be null,找了半天都找不出来问题所在,其他人又都可以启 ...

随机推荐

  1. Requests中文乱码解决方案

    分析: r = requests.get(“http://www.baidu.com“) **r.text返回的是Unicode型的数据. 使用r.content返回的是bytes型的数据. 也就是说 ...

  2. 「BZOJ3226」[Sdoi2008]校门外的区间

    题目 首先是开闭区间的处理,我们把\(1.5\)这种数加进来,用\([1.5,6]\)来表示\((2,6]\) 根据离散数学的基本知识,尝试把五个操作转化成人话 把\([x,y]\)变成\(1\) 把 ...

  3. 根据自增ID生成不重复序列号

    网上看到一个例子,源地址:https://www.aliyun.com/jiaocheng/536419.html 借鉴修改一下 实现根据long类型的用户ID生成6位随机邀请码,并且根据邀请码能算出 ...

  4. 随手练——HDU-1210 洗牌问题(模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1210 模拟的题目真不知道怎么写的话,就把真实情况展示出来,有图才有真相: 测试代码: #include ...

  5. robotframwork数据库测试(一)——pymysql与pyodbc

    本章节仅总结一下使用pymysql与pyodbc链接MySQL数据库. 2个都可以链接mysql数据,进行增删改查.但是呢,使用pyodbc操作数据库,对中文支持度好一些.pymysql则需要处理一下 ...

  6. centos上nginx的安装

    安装步骤:   1.下载nginx,执行:wget http://nginx.org/download/nginx-1.10.2.tar.gz 2.解压,执行:tar vxzf nginx-1.10. ...

  7. 如何求出数组中最小(或者最大)的k个数(least k问题)

    输入n个整数,如何求出其中最小的k个数? 解法1. 当然最直观的思路是将数组排序,然后就可以找出其中最小的k个数了,时间复杂度以快速排序为例,是O(nlogn): 解法2. 借助划分(Partitio ...

  8. 32位 的变量 用于表示 ms ,可以表示多少天那?

    1.在  TI  的 BLE 协议栈 中,即 OSAL 中 获取当前 系统 tick 的方法如下 /* * Read the system clock - returns milliseconds * ...

  9. Node 192.168.248.12:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

    [root@node00 src]# ./redis-trib.rb add-node --slave --master-id4f6424e47a2275d2b7696bfbf8588e8c4c3a5 ...

  10. css一边固定,另一边自适应的方法

    第一种: 第二种: