XPath's with String comparisons

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

  1. Guest

    Hello Everyone,

    I've looked through all the help files I can find, and as far as I
    know what Im doing is correct. I wrote a simple program that accepts an
    XPath from the command line and runs them against a set XML file. All
    the XPath's work great except when I compare the value of one tag
    against a string, and then it returns nothing.
    Check this out:

    XML:

    <cap>
    <a>
    <b> test </b>
    <c> test2 </c>
    </a>
    <a>
    <b> test3 </b>
    </a>
    </cap>

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

    public test(String q) {

    try { File impFile = new File("test.xml");
    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);

    } catch(Exception exc) {
    exc.printStackTrace(System.out);
    }
    }

    public void printNodes(NodeList nl) {
    if(nl == null) { return; }
    if(nl.getLength() == 0) { System.out.println("zero"); return; }
    for(int i=0; i<nl.getLength(); i++) {
    Node n = nl.item(i);
    System.out.println(n);

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

    return;
    }

    public static void main(String argv[]) {
    test t = new test(argv[0]);
    }

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

    When I run the program with the xPath query //a[b="test"] it returns
    nothing, when it should return the first <a> block. Any ideas? Any
    tag I try and check against a string variable like that returns
    nothing. When I try attributes @test="string" that works and returns,
    just not tag values.

    Thanks everyone
    , Jun 27, 2005
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > Hello Everyone,
    >
    > I've looked through all the help files I can find, and as far as I
    > know what Im doing is correct. I wrote a simple program that accepts an
    > XPath from the command line and runs them against a set XML file. All
    > the XPath's work great except when I compare the value of one tag
    > against a string, and then it returns nothing.
    > Check this out:
    >
    > XML:
    >
    > <cap>
    > <a>
    > <b> test </b>
    > <c> test2 </c>
    > </a>
    > <a>
    > <b> test3 </b>
    > </a>
    > </cap>
    >
    > When I run the program with the xPath query //a[b="test"] it returns
    > nothing, when it should return the first <a> block. Any ideas? Any
    > tag I try and check against a string variable like that returns
    > nothing. When I try attributes @test="string" that works and returns,
    > just not tag values.


    Are the extra spaces in

    <b> test </b>

    in the XML file, or were they added when composing your post? Without them,
    your XPath expression is correct.
    Mike Schilling, Jun 27, 2005
    #2
    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. Horn

    string comparisons

    Horn, Oct 3, 2003, in forum: C++
    Replies:
    7
    Views:
    375
    Ashish
    Oct 6, 2003
  2. Sam R
    Replies:
    1
    Views:
    507
  3. Nishad
    Replies:
    2
    Views:
    4,820
    Dimitre Novatchev
    Sep 30, 2006
  4. Merk
    Replies:
    12
    Views:
    1,386
    Jon Skeet [C# MVP]
    Dec 13, 2006
  5. Martin Honnen

    Re: XPath Comparisons

    Martin Honnen, Dec 15, 2008, in forum: XML
    Replies:
    1
    Views:
    756
    Martin Honnen
    Dec 15, 2008
Loading...

Share This Page