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 :
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 as below :
Step 2 :
Create package with name "com.gb.action".
Step 3 :
Create Class TestTagValue
Step 4 :
Remove the above code and paste following code :
package com.gb.action;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
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(
"C:/Users/testTagValue/abc.xml");
BufferedReader br = new BufferedReader(new InputStreamReader(
fstream));
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
System.out.println(fileName);
}
br.close();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
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 :
Any feedback ,suggestions ,questions on the post are welcome.
Sample Content of abc.xml :
<FileName>def.xml</FileName><DateTimeFile>20141126002523</DateTimeFile><DateTimeMessage>20141126002523</DateTimeMessage><MessageID>006743663</MessageID>
<FileName>ghi.xml</FileName><DateTimeFile>20141126011537</DateTimeFile><DateTimeMessage>20141126011537</DateTimeMessage><MessageID>006757690</MessageID>
<FileName>jkl.xml</FileName><DateTimeFile>20141126011535</DateTimeFile><DateTimeMessage>20141126011535</DateTimeMessage><MessageID>006757686</MessageID>
<FileName>mno.xml</FileName><DateTimeFile>20141126011538</DateTimeFile><DateTimeMessage>20141126011538</DateTimeMessage><MessageID>006757700</MessageID>
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 :
def.xml
ghi.xml
jkl.xml
mno.xml
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.