在使用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. DZ拿shell总结

    今天碰到一个dz的站,好久没拿了 ,拿下shell觉得应该总结一下 Uc_server默认密码 其实有了UC_SERVER就是有了网站的全部权限了,有了UC_SERVER你可以重置管理员密码 可以进后 ...

  2. C语言基础笔试题一

    1.下面的代码输出什么?为什么? void foo(void) { unsigned int a = 6; int b = -20; (a+b > 6)?puts(">6&quo ...

  3. [转]CentOS 7忘记root密码解决办法

    转自:http://www.linuxidc.com/Linux/2016-08/134034.htm  亲测可用! CentOS 7 root密码的重置方式和CentOS 6完全不一样,CentOS ...

  4. apache、nginx实现反向代理

    一.apache(不推荐): 代理80端口:a. 配置:b. 效果:c. 配置文件参考: ServerRoot "/etc/httpd" Listen 80 ProxyPass / ...

  5. 10、Web Service-IDEA-jaxrs 整合spring

    1.服务端的开发 1.web项目目录 2.pom <?xml version="1.0" encoding="UTF-8"?> <projec ...

  6. Owin+ASP.NET Identity浅析系列(一)用户登录注册

    在今天,读书有时是件“麻烦”事.它需要你付出时间,付出精力,还要付出一份心境.--仅以<Owin+ASP.NET Identity浅析系列>来祭奠那逝去的…… 使用VS2015创建MVC项 ...

  7. RAID基本知识

    RAID是英文Redundant Array of Independent Disks(独立磁盘冗余阵列),简称磁盘阵列.下面将各个级别的RAID介绍如下. 一.为什么使用Raid? 1.对磁盘高速存 ...

  8. maven加速镜像

    <mirror> <id>repo3</id> <mirrorOf>central</mirrorOf> <name>Human ...

  9. 通过nat让内网机器上网

    让内网机器访问不通机房的机器,架构如下 (172.16.10.5)  ----- (172.17.9.109 可以出外网,开启路由转发)  -------  (内网 192.168.36.19 能pi ...

  10. PHP-----PHP程序设计基础教程----第一章PHP开篇

    本章内容知识体系如下所示: 一.PHP基础知识 1.1 Web技术 服务器与客户端 服务器:能够提供某种服务的电脑 客户端:想使用服务器所提供服务的电脑 服务器也是一台电脑,必须安装操作系统.否则就是 ...