Solved

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

Posted on 2007-03-27
11
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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
mysql jsp example issue 32 69
Is Applet the way to go for my drag and drop system? 8 34
Html Table Looping (part 2) 5 34
junit as external jar or library 7 37
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

735 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