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.

XPath's with String comparisons

Thread view: 
gfrommer@hotmail.com - 27 Jun 2005 22:16 GMT
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
Mike Schilling - 27 Jun 2005 23:59 GMT
> Hello Everyone,
>
[quoted text clipped - 22 lines]
> 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.


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.