gpt4 book ai didi

java - XMLSerializer 抛出异常

转载 作者:行者123 更新时间:2023-12-02 07:43:49 25 4
gpt4 key购买 nike

我有一小段代码可以将 XML 解析为 JSON。它工作得很好,直到我引入 XMLSerializer 来获取 JSON。

public static String convertXMLFileToString(String fileName) 
{
try{
XMLSerializer xmlSer = new XMLSerializer();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
InputStream inputStream = new FileInputStream(new File(fileName));
org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream);
StringWriter stw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(doc), new StreamResult(stw));
String st = stw.toString();
JSON json = xmlSer.read(st);
return stw.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

通话:

convertXMLFileToString("/home/tom/workspace/xmlToJson/files/input/sample.xml");

我使用 JSON-lib-2.4 和以下导入 import net.sf.json.xml.XMLSerializer;我得到以下异常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at convert.xmlToJson.convertXMLFileToString(xmlToJson.java:27)
at convert.xmlToJson.main(xmlToJson.java:21)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 14 more

知道这里发生了什么吗?

最佳答案

JSON-lib 依赖于其他库,请参阅 http://json-lib.sourceforge.net/ .

  • Jakarta 公共(public)语言 2.5
  • Jakarta commons-beanutils 1.8.0
  • Jakarta commons-collections 3.2.1
  • Jakarta 公共(public)日志记录 1.1.1
  • ezmorph 1.0.6

不能说您是否需要所有这些,但您的异常清楚地表明它缺少 commons-lang ( http://commons.apache.org/lang/ )。确保使用 2.x 版本,而不是较新的 3.x。

关于java - XMLSerializer 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11210505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com