JDom working with XPath
Asked Answered
P

3

3

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);
    ......
}

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!

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;
Pung answered 14/8, 2011 at 16:40 Comment(3)
don't use the CLASSPATH env var, when they say include the jar in the classpath they mean add it to the java command using the -cp flag.Monson
Thanks, I typed this command: java -cp ../jaxen-1.1.1.jar : ../dom4j-1.6.1.jar:. xmlTohtml, it still throws exception...Pung
it's very odd you have dom4j in the classpath but are using jdom in your code. maybe it would help to show us the whole classpath.Monson
L
9

Include dom4j.jar and jaxen.jar in classpath.

Lovett answered 14/8, 2011 at 16:44 Comment(1)
I have added thw two files in my .cshrc file. setenv CLASSPATH ${CLASSPATH}:jaxen-1.1.1.jar setenv CLASSPATH ${CLASSPATH}:dom4j-1.6.1.jar, the two files are in the same directory as .cshrc file, but it still doesn't work...Pung
I
2

I suspect you don't have jaxen-1.1.3.jar on the classpath. If I were to assume you were running this from the command line, you'd want:

java -cp jaxen-1.1.3.jar:. myJavaProgram
International answered 14/8, 2011 at 16:51 Comment(1)
Thanks but its not working. I put jaxen and dom4j in my .cshrc file.Pung
U
-1

Just jaxen1.1.3 is sufficient and dom4j not required

Ultraism answered 27/2, 2013 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.