Solved

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

Posted on 2007-03-27
11
231 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I bind the results to a grid 3 34
Java DateChooser? 3 29
custom annotations 9 32
table example 4 23
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now