JDom使用XPath [英] JDom working with XPath
问题描述
我是JDom的新手,我尝试使用XPath访问我的xml文件。我的代码如下:
I am a newbie to JDom, I try to use XPath to access my xml file. My code is like following:
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编译该类时,它没有给出任何错误,但是当我尝试运行程序时,它会提供以下异常消息:
I have already imported org.jdom.xpath.XPath class. When I use javac to compile the class, it didn't give any error, however when I try to run the program, it gives the following exception message:
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
我错过了这里吗?任何帮助将不胜感激!
Did I miss something here? Any help will be greatly appreciated!
我将jaxen和dom4j添加到我的主目录下的.cshrc文件中,两个jar文件是一样的目录也是。
I add the jaxen and dom4j in my .cshrc file which resides directly under my home directory, and the two jar files are in the same directory too.
setenv CLASSPATH ${CLASSPATH}:/homedirectory/jaxen-1.1.1.jar
setenv CLASSPATH ${CLASSPATH}:/homedirectory/dom4j-1.6.1.jar
当我尝试此命令命令行,它会生成新的异常....
When I try this command from the command line, it generates new exceptions....
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)
顺便说一下,程序中的导入类是:
By the way, the imported classes in my program are:
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;
推荐答案
包含 dom4j.jar 和 jaxen。 jar 。
这篇关于JDom使用XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!