Hello daniweb,
I am new to java and am trying to wrap my head around parsing XML with Java. I've decided to try DOM with Xerces to start because it seems pretty simple, however I am getting NoClassDefFound errors. I'm sure it's something really simple that I have overlooked due to my inexperience with Java, but I have looked at multiple examples online and have followed their instructions point by point. I'm not doing all that much, so I'm not sure what could be wrong.
I have imported all the libraries that came with Xerces, which I downloaded off the Xerces website (Version 2.9.0). I am using NetBeans 6.7.1 as my IDE.
I have imported the following:
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
My code is fairly straightforward. I have my XML file (Test2_level.xml) in the same folder as my java class.
try {
DOMParser parser = new DOMParser();
parser.parse("Test2_level.xml");
Document doc = parser.getDocument();
NodeList nodes = doc.getElementsByTagName("servlet");
System.out.println("There are " + nodes.getLength() +
" elements.");
} catch (Exception ex) {
System.out.println(ex);
}
Here is the error that I keep getting.
Exception in thread "MyParser" java.lang.NoClassDefFoundError: org/apache/xerces/parsers/DOMParser
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getConstructor0(Class.java:2699)
at java.lang.Class.newInstance0(Class.java:326)
at java.lang.Class.newInstance(Class.java:308)
Caused by: java.lang.ClassNotFoundException: org.apache.xerces.parsers.DOMParser
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:319)
at java.lang.ClassLoader.loadClass(ClassLoader.java:254)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:399)
... 14 more