package com.xml2json;
import net.sf.json.JSONArray;
import net.sf.json.xml.XMLSerializer;
public class Xml2jsonDemo3 {
public static void xml2json(){
//模拟的xml文档
String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
xmlStr +="<family address=\"bj\" member=\"3\">";
xmlStr += "<myself age=\"18\" sex=\"male\">myname</myself>";
xmlStr += "<mother age=\"45\" sex=\"female\">mothername</mother>";
xmlStr += "<father age=\"46\" sex=\"male\">fathername</father>";
xmlStr += "</family>";
System.out.println(xmlStr);
//将xml文档转换成jsonArray对象
XMLSerializer xmls = new XMLSerializer();
JSONArray json = (JSONArray) xmls.read(xmlStr);
System.out.println(json.toString());
}
public static void main(String[] args) {
xml2json();
}
}
Exception in thread "main" java.lang.NoClassDefFoundError: nu/xom/Serializer
at com.xml2json.Xml2jsonDemo3.xml2json(Xml2jsonDemo3.java:17)
at com.xml2json.Xml2jsonDemo3.main(Xml2jsonDemo3.java:22)
Caused by: java.lang.ClassNotFoundException: nu.xom.Serializer
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
今天做一个小demo将xml文档转换成json对象,但是总是报这个错误,调了半天没调好,哪位大牛能给指导下。
------解决方案--------------------
它报的错是找不到nu.xom.Serializer这个类嘛,所以是缺xom-1.1.jar包。