How to access xml-values with XPath?
Hello (especially to Arne)!Your last svar really solved my problem but when I tried to run your program I ended up with this:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/BaseXPath
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at org.jdom.xpath.XPath.newInstance(XPath.java:126)
at JDomXPath.fillout(JDomXPath.java:20)
at JDomXPath.main(JDomXPath.java:27)
Press any key to continue . . .
The code that you gave me was:
import java.io.IOException;
import java.io.StringReader;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;
public class JDomXPath {
private static final String template = "<protocol>" +
"<type></type>" +
"<version></version>" +
"<commands></commands>" +
"</protocol>";
public static void fillout(Document doc, String tag, String value) throws JDOMException
{
XPath xpath = XPath.newInstance("/");
Element elm = (Element) xpath.selectSingleNode(doc, "protocol/" + tag);
elm.setText(value);
}
public static void main(String[] args) throws JDOMException, IOException {
SAXBuilder b = new SAXBuilder();
Document doc = b.build(new StringReader(template));
fillout(doc, "type", "Dette er en type");
fillout(doc, "version", "Dette er en version");
fillout(doc, "commands", "Dette er en commands");
XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
System.out.println(fmt.outputString(doc));
}
}
Perhaps I need some more jar-files? I just got jdom.jar from www.jdom.org. But the strange thing is that I can compile it with out any problems, but when I try to run it I get that execption. To me that means I do not have the special class needed.
I use:
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)
... if that matters?
So if you could explain this and perhaps tell me where I a found any jar-files that I need to download please let me know!
Best regards
Fredrik
