Take the 2-minute tour ×

I have found similar questions to mine, but just not exactly the same.

You see, what i am trying to make is a program that can search through a range og XML files in a directory and tell me which xml files contains a given string..

As an example: i search for "sexy and i know it" and the program will tell me "4.xml" because there is written "sexy and i know it" in that XML file, i have found a way to identify all the XML files, but now i just need some code that can search through these XML files and give output the title of the XML files which contains my string.

Code for the definition i have made like this:

String path = "C:/Users/Daniel/Desktop/CD";
File folder = new File(path);
File[] listOfFiles = folder.listFiles();

share|improve this question
What is the similar question. Also, what do you expect if you have something like: <p>sexy and <b>i know it</b></p>. Have you looked at XPATH expressions? –  Jay Elston Nov 23 '11 at 17:00
add comment

1 Answer

up vote 1 down vote accepted

Raw File IO

This method does not take into account any XML tags or anything. It simply checks for a given string contained in a file

The basic steps you have to do are using bare files:

  1. Loop through the files in listOfFiles
  2. Open each file (see FileReader and BufferedReader)
  3. Read the content (e.g. line by line with readLine)
  4. Check if your string is in the read content
  5. If yes, output the file's name and continue with next file

or in minimal code:

// In looping through files:
// Step 2:
BufferedReader r = new BufferedReader(new FileReader(f)); 
// Step 3:
String s = r.readLine();
while (s != null) {
    // Step 4:
    if (s.contains("TestString")) {
        // Step 5:
    s = r.readLine();

Note that this won't work if there are linebreaks in your string!


SAX is a stream oriented XML parser standard that reads XML and gives callbacks to a handler if e.g. an element starts, text is found, etc. in the order they occur. You can use this for checking the text contents in the XML, and also implement a stateful handler that tracks if the text is really encountered in a <string> element, but this is a bit more complex.

This is what to do using a SAX2 implementation:

  1. Implement the ContentHandler SAX interface in your class
  2. In that content handler, implement the characters method to check for your string
  3. Create a reader with XMLReaderFactory.createXMLReader()
  4. Set your content handler with setContentHandler on the reader
  5. Call parse on the reader with an InputSource for your file


DOM is a standard XML model that builds a tree of the nodes in memory that can be traversed. This method is really simple to use:

  1. Parse the XML into a DOM using DocumentBuilder
  2. Get all <string> elements using getElementsByTagName
  3. Check if their values equal your test string using getNodeValue


Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(f);
NodeList l = d.getElementsByTagName("string");
for (int i = 0; i < l.getLength(); ++i) {
    if ("TestString".equals(l.item(i).getNodeValue())) {

Hope this helps.

share|improve this answer
Yeah i got to that point aswell hehe.. my problem is that i do not know how to code that, i know how to make the loop but not how to make the filereader do exactly what i am asking for, as it usually looks for tags and then prints the tag, but i just need to search for a string –  Exasis Nov 23 '11 at 10:05
The file reader doesn't know about XML, so it will read the raw content. You could use String.contains if that is enough for your usecase. Otherwise check my additional comments on SAX, as this uses a standard XML reader interface and will only check for the text outside of tags –  king_nak Nov 23 '11 at 10:13
Well the text is inside a tag called <string> </string>, but the point is that the examples given on the net, they use the parsers to recognize and print tags, i just need to search the text for a given string. As i am not that good with java, that i can use the filereader directly from what oracle tells, would it be possible for you to guide me on to how this would work? its my first time reading external files from a program and this filereader stuff seems quite confusing to me :) –  Exasis Nov 23 '11 at 10:23
add comment

Your Answer


