Avatar billede fredand Forsker
10. april 2005 - 17:12 Der er 4 kommentarer og
1 løsning

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
Avatar billede arne_v Ekspert
10. april 2005 - 17:15 #1
You need all of:

jdom.jar
jaxen-core.jar
jaxen-jdom.jar
saxpath.jar

in classpath to run the code !
Avatar billede arne_v Ekspert
10. april 2005 - 17:15 #2
The last 3 comes with JDOM and are in the lib directory.
Avatar billede fredand Forsker
10. april 2005 - 19:13 #3
Hello!
Works absolutely perfect!

Just give me a "svar" so I can reward you mate!

Best regards
Fredrik
Avatar billede arne_v Ekspert
10. april 2005 - 19:15 #4
.
Avatar billede fredand Forsker
10. april 2005 - 19:24 #5
Thanks mate!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester