Simple XPATHs arent working... HELP!

Discussion in 'Java' started by gfrommer@hotmail.com, Jun 29, 2005.

  1. Guest

    Hello Everyone,

    I am having some very strange results using the XPath classes
    included with the JDK1.5.0_01. Check this out:

    XML:

    <final>
    <a>
    <b>1</b>
    <b>2</b>
    </a>
    <a>
    <b>3</b>
    <b>4</b>
    </a>
    </final>

    Code:

    DocumentBuilder db =
    DocumentBuilderFactory.newInstance().newDocumentBuilder();

    Document impDoc = db.parse(impFile);
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nl = (NodeList) xpath.evaluate(q, impDoc,
    XPathConstants.NODESET);

    printNodes(nl);

    public void printNodes(NodeList nl) {
    if(nl == null) { return; }
    if(nl.getLength() == 0) { return; }

    for(int i=0; i<nl.getLength(); i++) {
    Node n = nl.item(i);

    if(n.getNodeValue() != null &&
    n.getNodeValue().toString().trim().length() > 0)
    System.out.println(n.getNodeValue());

    NodeList nl2 = n.getChildNodes();
    printNodes(nl2);
    }

    return;
    }


    --------------------------------

    When I run the XPAth query: //b[.=2] That returns the proper <b> node.
    But if I do //b[.>2] or //b[b >= 1] or //b[. != 1] .... all of those
    return nothing even though there are valid results. Whenever I compare
    on a string it always returns nothing. Are the XPath classes in the
    JDK1.5 buggy? Should I be using a different library?

    Im really confused.... simple xPaths without any []'s always seem to
    work, but the node tests just dont want to work.

    Any ideas?
    , Jun 29, 2005
    #1
    1. Advertising

  2. wrote:
    > Hello Everyone,
    >
    > I am having some very strange results using the XPath classes
    > included with the JDK1.5.0_01. Check this out:
    >
    > XML:
    >
    > <final>
    > <a>
    > <b>1</b>
    > <b>2</b>
    > </a>
    > <a>
    > <b>3</b>
    > <b>4</b>
    > </a>
    > </final>
    >
    > Code:
    >
    > DocumentBuilder db =
    > DocumentBuilderFactory.newInstance().newDocumentBuilder();
    >
    > Document impDoc = db.parse(impFile);
    > XPath xpath = XPathFactory.newInstance().newXPath();
    > NodeList nl = (NodeList) xpath.evaluate(q, impDoc,
    > XPathConstants.NODESET);
    >
    > printNodes(nl);
    >
    > public void printNodes(NodeList nl) {
    > if(nl == null) { return; }
    > if(nl.getLength() == 0) { return; }
    >
    > for(int i=0; i<nl.getLength(); i++) {
    > Node n = nl.item(i);
    >
    > if(n.getNodeValue() != null &&
    > n.getNodeValue().toString().trim().length() > 0)
    > System.out.println(n.getNodeValue());
    >
    > NodeList nl2 = n.getChildNodes();
    > printNodes(nl2);
    > }
    >
    > return;
    > }
    >
    >
    > --------------------------------
    >
    > When I run the XPAth query: //b[.=2] That returns the proper <b> node.
    > But if I do //b[.>2] or //b[b >= 1] or //b[. != 1] .... all of those


    > en < or unkown in xpath, you should use &lt; and &gt;

    For != im not sure, maye not(.=1) works?

    > return nothing even though there are valid results. Whenever I compare
    > on a string it always returns nothing. Are the XPath classes in the
    > JDK1.5 buggy? Should I be using a different library?
    >
    > Im really confused.... simple xPaths without any []'s always seem to
    > work, but the node tests just dont want to work.
    >
    > Any ideas?
    >
    Tjerk Wolterink, Jun 29, 2005
    #2
    1. Advertising

  3. Guest

    I tried it with &lt; and &gt; and both dont work. They each throw an
    "javax.xml.transform.TransformerException: Expected ], but found: &lt;"
    error.
    Why are these simple xpaths not working? There isnt much to go wrong in
    the code, it has to be Java's XPath engine.
    , Jun 29, 2005
    #3
  4. Guest

    Also, when I run the XPath with <= it simply returns nothing... when I
    use the &lt; or &gt; its throwing an exception. Very confusing.
    , Jun 29, 2005
    #4
  5. Guest

    Hmph.... html is interpreting the < and > with those symbols you
    suggested.... thats what I tried...
    , Jun 29, 2005
    #5
  6. Guest

    I figured it out... the DOS window was misinterpreting my command line
    parameters. I like how I immediatly faulted Java's XPath engine
    though.... it's not my problem, the universe is wrong :)

    Peace
    , Jun 30, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kathy Burke
    Replies:
    0
    Views:
    328
    Kathy Burke
    Jul 31, 2003
  2. Replies:
    2
    Views:
    1,914
    Ian Shef
    Apr 20, 2005
  3. Patrick TJ McPhee
    Replies:
    5
    Views:
    400
    C. M. Sperberg-McQueen
    Apr 6, 2004
  4. Andy B

    textbox values arent showing up?

    Andy B, Jan 25, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    368
    Jignesh
    Jan 25, 2008
  5. Coolgg
    Replies:
    9
    Views:
    154
    Coolgg
    Jan 24, 2013
Loading...

Share This Page