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,找了半天都找不出来问题所在,其他人又都可以启 ...
随机推荐
- windows server 2008/2012安装PostgreSQL过程及问题总结
今天第一次接触在Windows Server2008/2012 下安装PostgreSQL,遇到的问题总结如下: 1.首先,在安装的时候,我更改安装目录改为自定义的E盘,安装时报错:libintl.d ...
- 给trac的ticket添加提交时字段验证
我们在项目管理中使用了trac系统,并且对于ticket添加了以下自定义字段并且对它们的格式都有一定要求: svn版本号:格式为 r1234.多个版本号之间使用半角逗号隔开.如:r1234,r5678 ...
- TensorFlow函数(二)tf.get_variable() 和 tf.Variable()
tf.Variable(<initial - value>,name=<optional - name>) 此函数用于定义图变量.生成一个初始值为initial - value ...
- Python常用库之三:Matplotlib
导入模块 import matplotlib.pyplot as plt import seaborn as sb 绘制条形图 countplot(data:数据集, x:x坐标轴, color:条形 ...
- $_cookie的使用
设置并发送 cookie: <?php $value = "my cookie value"; // 发送一个简单的 cookie setcookie("TestC ...
- 新版剑指offer14 剪绳子
int maxProduct(int length){ ) ; ) ; ) ; ; == ) numof3 -= ; )/; ,numof3))*(,numof2)); }
- Yum 下载安装包及对应依赖包
Yum 下载安装包及对应依赖包: 安装该软件:yum install -y yum-plugin-downloadonly 以下载 openssh-server 为例 yum install -y o ...
- PAT——1032. 挖掘机技术哪家强
为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第1行给出不超过105的正整数N,即参赛人数.随后N行,每行给出一位 ...
- mysql 5.7设置密码无效
我现在MySQL的版本时8.0.12,以前一直没有给MySQL设置密码. 今天因为需要,给MySQL设置,密码,但是上网搜了好久.....命令都不对.最后搜到csdn的Bpazy大佬的博客.他使用5. ...
- Hadoop相关笔记
一. Zookeeper( 分布式协调服务框架 ) 1. Zookeeper概述和集群搭建: (1) Zookeeper概述: Zookeeper 是一个分布式 ...