Solved

Fast way to test for a text value in a node among thousands of nodes

Posted on 2007-03-27
11
238 Views
Last Modified: 2010-03-31
I wanted to know if there's a quick way in Java to check and see if a value exists in a text node.  I have the following XML structure where I will need to see if a url already exists in one of the file nodes.  Is there a way to do this magically ;-) , or do I have to grab a handle to the <metadata-files> node, and traverse through each file/url node:

<main>
       <metadata-files>
                <file>
                        <url>http://www.experts-exchange.com</url>
                 </file>
                <file>
                        <url>http://www.experts-exchange.com</url>
                 </file>
                <file>
                        <url>http://www.experts-exchange.com</url>
                 </file>
        </metadata-files>
<main>

0
Comment
Question by:PurpleSlade
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
ID: 18803261
XPath ;-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18803301
You don't need to traverse if you use XPath as CEHJ says. You can use selectSingleNode () to do that.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 200 total points
ID: 18803307
(that is the Apache XPath API.)

You can also use the Java XPath API: http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/xpath/package-summary.html
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 2

Author Comment

by:PurpleSlade
ID: 18803374
I have this strange feeling that CEHJ is trying to tell me something, but I just can't put my finger on it ....

OK, if I use XPath could you give me an example of using it to test to see if "http://www.experts-exchange.com" is in one of the text nodes based on the above xml?
0
 
LVL 13

Assisted Solution

by:kawas
kawas earned 150 total points
ID: 18803465
String xpath  = "//*[.='http://www.experts-exchange.com']";
0
 
LVL 13

Expert Comment

by:kawas
ID: 18803472
0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 18803525
Also, if I don't use Xpath and want to traverse the document, is this the way to find the text node of hte url element?

                        NodeList filenodes = doc.getElementsByTagName("file");
                        System.out.println("filenodes has " + filenodes.getLength() + " children.");

                        boolean found=false;
                        for(int i=0; i < filenodes.getLength(); i++)
                        {
                              if( filenodes.item(i).getFirstChild().getTextContent().equals(URL) )
                              {
                                    System.out.println("FOUND: " + URL);
                                    found=true;
                              }
                        }
0
 
LVL 13

Expert Comment

by:kawas
ID: 18803544
looks right to me, but you posted it for a reason ... what's wrong?
0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 18803630
It wasnt' working and I got null pointer exceptions.  This ended up working, just getting the url tags rather than grabbing the parent of the url.

                        NodeList urlnodes = doc.getElementsByTagName("url");

                        boolean found=false;
                        for(int i=0; i < urlnodes.getLength(); i++)
                        {
                              if( urlnodes.item(i).getTextContent().equals(URL) )
                              {
                                    // System.out.println("FOUND: " + URL);
                                    found=true;
                              }
                        }
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18803833
You can also use getElementsByTagName () as I had shown in your earlier question.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18814443
:-)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tutorial for ebay api 3 55
VB.Net. Reading xml value 6 30
Need Help! Getting a syntax error and don't understand why 3 31
How to implement an additional command 16 40
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question