Ho questa classe java, il file si chiama DescendantDemo.java:
codice:
package xml;
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.xpath.XPath;
public class DescendantDemo {
public static void main(String[] args) throws Exception {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build("src/xml/web.xml");
XPath x = XPath.newInstance("/collection/dvd/@id");
List list = x.selectNodes(doc);
listAttributes(list, "");
}
private static void listElements(List es, String indent) {
for (Iterator i = es.iterator(); i.hasNext();) {
Element e = (Element) i.next();
listElement(e, indent);
}
}
private static void listElement(Element e, String indent) {
System.out.println(indent + "*Element, name:" +
e.getName() + ", text:" +
e.getText().trim());
//List all attributes
List as = e.getAttributes();
listAttributes(as, indent + " ");
//List all children
List c = e.getChildren();
listElements(c, indent + " ");
}
private static void listAttributes(List as, String indent) {
for (Iterator i = as.iterator(); i.hasNext();) {
Attribute a = (Attribute) i.next();
System.out.println(indent + "*Attribute, name:" +
a.getName() + ", value:" +
a.getValue());
}
}
}
E il documento XML (web.xml):
codice:
<?xml version="1.0" encoding="UTF-8"?>
<collection>
<dvd id="A">
<title>Lord of the Rings: The Fellowship of the Ring</title>
<length>178</length>
<actor>Ian Holm</actor>
<actor>Elijah Wood</actor>
<actor>Ian McKellen</actor>
</dvd>
<dvd id="B">
<title>The Matrix</title>
<length>136</length>
<actor>Keanu Reeves</actor>
<actor>Laurence Fishburne</actor>
</dvd>
<dvd id="C">
<title>Amadeus</title>
<length>158</length>
<actor>F. Murray Abraham</actor>
<actor>Tom Hulce</actor>
<actor>Elizabeth Berridge</actor>
</dvd>
<dvd id="D">
<title>Chain Reaction</title>
<length>106</length>
<actor>Morgan Freeman</actor>
<actor>Keanu Reeves</actor>
</dvd>
</collection>
Quando compilo mi dice:
codice:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
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 xml.DescendantDemo.main(DescendantDemo.java:21)punta alla riga in rosso
Java Result: 1
Dove è l'errore???