Solved

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

Posted on 2007-03-27
11
227 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:mayankeagle
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:
mayankeagle 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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:mayankeagle
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

743 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

9 Experts available now in Live!

Get 1:1 Help Now