Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Simple XPATHs arent working... HELP!

Thread view: 
gfrommer@hotmail.com - 29 Jun 2005 18:36 GMT
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?
Tjerk Wolterink - 29 Jun 2005 21:31 GMT
> Hello Everyone,
>
[quoted text clipped - 48 lines]
> 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
[quoted text clipped - 4 lines]
>
> Any ideas?
gfrommer@hotmail.com - 29 Jun 2005 22:02 GMT
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.
gfrommer@hotmail.com - 29 Jun 2005 22:05 GMT
Also, when I run the XPath with <= it simply returns nothing... when I
use the &lt; or &gt; its throwing an exception. Very confusing.
gfrommer@hotmail.com - 29 Jun 2005 22:18 GMT
Hmph.... html is interpreting the < and > with those symbols you
suggested.... thats what I tried...
gfrommer@hotmail.com - 30 Jun 2005 00:13 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.