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对象与内存控制
1.1 实例变量和类变量 成员变量和局部变量: 局部变量分为三大类: 1)形参:在方法签名中定义的局部变量,由方法调用者为其赋值,随方法的结束而消亡. 2)方法内的局部变量:在方法内定义的局部变量,随 ...
- jQuery UI 实例 – 切换(Toggle)
toggle()函数用于为每个匹配元素的click事件绑定轮流的处理函数. toggle()是一个特殊的事件函数,用于为匹配元素的click事件绑定多个事件处理函数.每次触发click事件时,togg ...
- vim 取消高亮
/ 匹配以后会一直有高亮,退出以后还是有: set nohlsearch nohlsearch
- 【Vue.js】高仿饿了么外卖App(一)
1.架构从传统的MVC向REST API+前端MV*迁移 参考链接: http://blog.csdn.net/broadview2006/article/details/8615055 http:/ ...
- 【CSS-flex】圣杯布局(Holy Grail Layout)、输入框的布局、悬挂式布局、固定的底栏
1.圣杯布局(Holy Grail Layout) 其指的是一种最常见的网站布局.页面从上到下,分成三个部分:头部(header),躯干(body),尾部(footer).其中躯干又水平分成三栏,从左 ...
- linux下安装php扩展amqp
1 安装扩展必要依赖 rabbitmq-c 安装包地址:https://github.com/alanxz/rabbitmq-c/releases wget -c https://github.com ...
- Oracle 表空间、段、区和块简述
数据块(Block) 数据块Block是Oracle存储数据信息的最小单位.注意,这里说的是Oracle环境下的最小单位.Oracle也就是通过数据块来屏蔽不同操作系统存储结构的差异.无论是Windo ...
- canvas制作的烟花效果
最近感觉canvas挺有意思的,在业余时间没事研究了一下,参考过网上一些思路,话不多说,开始啦. github地址:https://github.com/aWhiteBear/fireworks 演示 ...
- python2.7+pyqt+eric基本控件操作
一.单选按钮被选中 1.选择 Radio Button 2.加入如下代码: #单选按钮状态切换时触发onstateChanged函数 self.radioButton.toggled.connect( ...
- linux 学习第十一天
一.配置服务说明 1.1.linux系统中的一切都是文件 1.2.配置一个服务就是在修改去配置文件 1.3.要想让新的配置文件立即生效,需要重启对应的服务 二.配置网卡 2.1.编辑配置文件 vim ...