我是JDom的新手,我尝试使用XPath来访问我的xml文件。我的代码如下:
public static void main(String[] args) throws Exception {
Document doc = new SAXBuilder().build("file.xml");
XPath x = XPath.newInstance("xpath");
//select the first element in the nodeset
Element elem = (Element)x.selectSingleNode(doc);
......
}
我已经导入了org.jdom.xpath.XPath类。当我使用javac编译类时,它没有给出任何错误,但是当我尝试运行程序时,它会给出以下异常消息:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/NamespaceContext
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.jdom.xpath.XPath.newInstance(XPath.java:134)
at myclassname.main(xmlTohtml.java:18)
Caused by: java.lang.ClassNotFoundException: org.jaxen.NamespaceContext
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
... 4 more
我在这里错过了什么吗?任何帮助将不胜感激!
我在我的.cshrc文件中添加了jaxen和dom4j,该文件直接位于我的主目录下,两个jar文件也在同一个目录中。
setenv CLASSPATH ${CLASSPATH}:/homedirectory/jaxen-1.1.1.jar
setenv CLASSPATH ${CLASSPATH}:/homedirectory/dom4j-1.6.1.jar
当我从命令行尝试此命令时,它会生成新的异常....
Exception in thread "main" java.lang.NoClassDefFoundError: :
Caused by: java.lang.ClassNotFoundException: :
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
顺便说一句,我程序中导入的类是:
import java.util.Iterator;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.Namespace;
import org.jdom.xpath.XPath;