How to get value of xml tags from a File

Hi Friends,

Sometime back I cam across situation where I had a very big file(it was log file actually) having multiple xml requests on multiple lines,so that means one xml request per line and I wanted to get values from one particular tag from each request.So I wrote program mentioned in below post,which gave me FileNames from each request.

Let us see now how we can get value of  xml tags from a big file.

Prerequisite :
IDE : Eclipse or any other IDE for java development.
JDK : I used JDK6
commons-lang.jar(I have used commons-lang-2.6.jar)

Step 1 : Create a new java project in eclipse

Step 2 : Create package with name

Step 3 : Create Class TestTagValue

Step 4 : Remove the above code and paste following code :



import org.apache.commons.lang.StringUtils;

public class TestTagValue {

public static void main(String args[]) {
// Open the file
FileInputStream fstream = null;
try {
fstream = new FileInputStream(
BufferedReader br = new BufferedReader(new InputStreamReader(
String strLine;

// Read File Line By Line
while ((strLine = br.readLine()) != null) {
String fileName = StringUtils.substringBetween(strLine,
"<FileName>", "</FileName>");
// Print the content on the console
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block

} catch (IOException e1) {
// TODO Auto-generated catch block


Step 5 : You will following compilation errors :

Step 6 : Add commons-lang jar on build path and errors will be gone

Step 7 : Place  your abc.xml file at C:/Users/testTagValue or your preferred location(but same path you need to mention in the program above)

Sample Content of abc.xml :


Step 8: Right click anywhere on the class and select Run As -> Java Application.
It will give you content of tags <FileName> in console.

Output :


Note : If you want to have content of some other tag ,you just need to change the tag name in above program.

Any feedback ,suggestions ,questions on the post are welcome.