The program below reads xml files from specific directory, extracts a value out of each one of them using XPath expression,
puts the value side by side with a substring of their fiilename and sends this output to a file:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.StringTokenizer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class ReadXML {
//Set the directory:
final static File folder = new File("C:/Reports/all_exceptions_10JUL2013");
//Set the single xml repository:
private Document document;
public static void main(String[] args) {
ReadXML readXML = new ReadXML();
readXML.listFilesForFolder(folder);
}
public void parseXML(File file) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
//document = db.parse(new InputSource(new StringReader(xml)));
document = db.parse(file);
}
public void listFilesForFolder(final File folder) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db=null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
}
FileWriter fstream=null;
try {
fstream = new FileWriter("C:\\Reports\\all_exceptions_10JUL2013\\exception_files_table.txt");
} catch (IOException e1) {
e1.printStackTrace();
}
BufferedWriter bfw = new BufferedWriter(fstream);
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
if(fileEntry.getName().substring(fileEntry.getName().length()-4).equals(".xml")){
try {
document = db.parse(fileEntry);
} catch (Exception e) {
e.printStackTrace();
}
//Initialize XPathFactory:
XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();
String text="";
try {
text = xp.evaluate("//Request/XMLParameter/RequestID/text()", document.getDocumentElement());
} catch (XPathExpressionException e) {
e.printStackTrace();
}
//Extract the submissionId from the filename:
StringTokenizer stringPiece = new StringTokenizer(fileEntry.getName(), "-");
int i=0;
String submissionId = null;
while (stringPiece.hasMoreElements()) {
submissionId = stringPiece.nextElement().toString();
i++;
if (i==2) break;
}
//Print it in table format:
//System.out.println("select '" + submissionId + "' as request_id, '" + text + "' as request_id from dual--" + fileEntry.getName());
//System.out.println("union");
try {
bfw.write("select '" + submissionId + "' as submission_id, '" + text + "' as request_id from dual--" + fileEntry.getName());
bfw.write("\r\n");
bfw.write("union");
bfw.write("\r\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
try {
bfw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String extractedXMLValue(String path){
String theValue = null;
return theValue;
}
}