[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

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

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
PurpleSlade
Asked:
PurpleSlade
  • 3
  • 3
  • 3
  • +1
3 Solutions
 
CEHJCommented:
XPath ;-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You don't need to traverse if you use XPath as CEHJ says. You can use selectSingleNode () to do that.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
(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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PurpleSladeAuthor Commented:
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
 
kawasCommented:
String xpath  = "//*[.='http://www.experts-exchange.com']";
0
 
kawasCommented:
0
 
PurpleSladeAuthor Commented:
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
 
kawasCommented:
looks right to me, but you posted it for a reason ... what's wrong?
0
 
PurpleSladeAuthor Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
You can also use getElementsByTagName () as I had shown in your earlier question.
0
 
CEHJCommented:
:-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now