XML2JSON 的【net.sf.json.JSONException: nu.xom.ParsingException must be followed by either attribute specifications, ">" or "/>"】问题解决办法
在使用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 "/>"】问题解决办法的更多相关文章
- Json_异常_net.sf.json.JSONException: JSONObject["solution"] not found.
net.sf.json.JSONException: JSONObject["solution"] not found. 没有这个元素造成的. 问题代码: JSONObject j ...
- 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跟个,,要不个哪的对象系列 ...
- net.sf.json.JSONException: java.lang.NoSuchMethodException
在尝试将json对象转换为list时候出现了如下错误 Exception in thread "main" net.sf.json.JSONException: java.lang ...
- json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...
- net.sf.json.JSONException: There is a cycle in the hierarchy!
因为项目中使用了AJAX技术,jar包为:json-lib.jar,在开发过程中遇到了一个JSON-LIB和Hibernate有关的问题: 如hibernate延迟加载错误,这都是些老问题了,一看就知 ...
- json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
转:json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 执行:JSONArray arr ...
- net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案 今天在用List集合转换成json数组的时候发生了这个错误,这个 ...
- 前后台JSON传值得一个问题和异常处理net.sf.json.JSONException: Unquotted string '"name"'
项目中做导入的时候遇到个bug,用JSON.stringify()序列号json对象传给后台:然后后台通过getPatameter()获取值时,前台的英文引号变成了中文引号. 原来代码如下:(自己排查 ...
- 找了两个小时的错误,net.sf.json.JSONException: JSON keys cannot be null.
因为数据库里面一条记录插入的是NULL,所以导致报了net.sf.json.JSONException: JSON keys cannot be null,找了半天都找不出来问题所在,其他人又都可以启 ...
随机推荐
- java多态-向上转型和向下转型
向上转型:符合“is a”,是安全的,子类向上到父类,多余的属性和方法会丢弃 向下转型:不安全的,用instanceof提前判断一下,以免抛出异常 instanceof用法: result = obj ...
- MongoDB authentication failed
0.随笔摘要: MongoDB 安装配置 MongoDB 权限控制 MongoDB 注意事项 authentication failed 1.MongoDB 下载安装配置 MongoDB官网 ...
- BZOJ 3224 Tyvj 1728 普通平衡树模板
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 题目大意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以 ...
- POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】
题目冲鸭:http://poj.org/problem?id=1743 Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Su ...
- 5、Spring-Cloud-声明式调用 Feign(上)
5.1.写一个 Feign 害户端 新建项目: 依赖: <dependency> <groupId>org.springframework.boot</groupId&g ...
- [LuoguP4711]分子质量(小模拟+玛丽题)
--这个题我居然可以把他\(1A\)--真是让我不知其可\(qwq\) \(Link\) \(emmmm\)好像发现了什么固定的套路(?)-- 大概就是这种题总会有 1.读入数 方案:快读即可. 2. ...
- 框架 hibernate3 多条查询 分页
package com.hanqi.test; import java.util.Date; import java.util.List; import org.hibernate.Session; ...
- windows下nginx访问web目录提示403 Forbidden
在windows下 http服务器nginx时,访问web目录提示403 Forbidden,首先需要了解nginx出现403错误是什么意思: 403 Forbidden表示你在请求一个资源文件但是n ...
- cocoscreator 2.04 配置 visual code 断点调试
1,cocoscreator ,chrome浏览器,visual code 这三个软件的安装 2,官网配置visual code 环境 https://docs.cocos.com/creator/m ...
- standby_file_management 参数为manual 导致ORA-01111问题
情景: Dataguard 物理备库执行恢复报错: Errors in file /home/u01/app/diag/rdbms/rzorcl11g/ORCL/trace/ORCL_pr00_358 ...