import java.io.File; import java.util.List; import org.dom4j.Document; import org.dom4j.io.SAXReader; public class Test { public static void main(String args[]) { try { File f = new File("D:/data.xml"); if (f.exists()) { SAXReader reader = new SAXReader(); reader.setEncoding("UTF-8"); Document document = reader.read(f); if (document == null) { System.out.println("hehe"); }else{ System.out.println(document.content()); <b>List list = document.selectNodes("//root/author");</b> System.out.println(list.size()); } }else System.out.println("file not exist"); } } catch (Exception e) { e.printStackTrace(); } } }
xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <root name="max"> <author name="James" location="UK">James Strachan</author> <author name="Bob" location="US">Bob McWhirter</author> </root>
webService客户端程序:
import java.net.MalformedURLException; import java.net.URL; import org.codehaus.xfire.client.Client; public class WebServiceClient { private static Client client; public static void main(String[] args) { try { client = new Client(new URL("http://127.0.0.1:8080/services/sczxService.ws?wsdl")); Object[] o=client.invoke("searchScjgBySjh", new Object[]{"13926275980"}); System.out.println(o[0].toString()); client.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
运行客户端程序时报错:
[org.dom4j.tree.DefaultElement@7a84e4 [Element: <root attributes: [org.dom4j.tree.DefaultAttribute@1aaa14a [Attribute: name name value "max"]]/>]]
java.lang.NoClassDefFoundError: org/jaxen/JaxenException
at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)
at test2.Test.main(Test.java:21)
错误出在List list = document.selectNodes("//root/author");这里,但我不知道为什么错
------解决方法--------------------------------------------------------
少包 jaxen.jar
------解决方法--------------------------------------------------------
同上,在dom4j的解压包lib目录下把jaxen.jar放到你的类路径里去
总结java.lang.NoClassDefFoundError可能有俩种:
1.java.lang.NoClassDefFoundError指向的"org/jaxen/JaxenException "这个字符串所指对象的类不存在
如不是自定义类的话,到 http://www.findjar.com 查找包含该信息的jar包并引入.
2.类的加载先后顺序有问题
如启动web application的时候,类的加载是有一个先后顺序的,如果应该先加载的类在后面加载,别的类在调用时找不到它,那么也会报这个错.
其他 java.lang.NoClassDefFoundError 异常处理都可这样处理.