My Source xml file looks like this
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE DocName PUBLIC "-//msg//msg1 Project_Name 1.1//EN" "My_Project_Name_V1_1.dtd">
<My_Project_Name dtdVersion="V1_1" fileName="Guidance_Document_SQL" softwareName="prototype" softwareVersion="0.1" productionDate="2012-01-02">
<ApplicantFileReference>ABCD#1234</ApplicantFileReference>
<ApplicantName languageCode="EF">Michael Smith</ApplicantName>
<ApplicantNameLatin>Michael Smith </ApplicantNameLatin>
<ProductTitle languageCode="EF">Some Example </InventionTitle>
<TotalQuantity>88</TotalQuantity>
<Example_Data exampleIDNumber="1">
<Exm_Seq>
<Exm_Seq_length>7</Exm_Seq_length>
<Exm_Seq_type>MM</Exm_Seq_type>
<Exm_Seq_div>PAT</Exm_Seq_div>
<Exm_Seq>
</Example_Data>
I am splitting this file and creating 2 files. One is .header file and other is .body file. Body file will start from "Example_Data" tag. Now the problem is when the .body file is created the content is creating right from the start of the file without considering the spaces. Like following:
<Example_Data exampleIDNumber="1">
<Exm_Seq>
<Exm_Seq_length>7</Exm_Seq_length>
<Exm_Seq_type>MM</Exm_Seq_type>
<Exm_Seq_div>PAT</Exm_Seq_div>
<Exm_Seq>
</Example_Data>
But I want to consider the spaces too so the content in the body file starts from the position it has in the original file(after 4 spaces or whatever number of spaces before the Example_Data tag has. I can hardcode for 4 spaces but it's not going to help my cause because there are other files where there could be more spaces before this tag).
Here is the piece of code I am working on for the splitting:
public class Splitter {
public static void main(String[] args) {
String charset = "UTF-8";
String original = args[0];
String stem = original.substring(0, original.length() - 4);
String headName = stem + ".head";
String bodyName = stem + ".body";
String bodyStart ="<Example_Data";
try {
//get rid of existing split files
File existing = new File(headName);
if(existing.exists()){
existing.delete();
System.out.println("Old header File has been deleted");
}
existing = new File(bodyName);
if(existing.exists()){
existing.delete();
System.out.println("Old body file has been deleted");
}
//read in original file
StringBuilder fileData = new StringBuilder(1000);
FileInputStream fis = new FileInputStream(original);
InputStreamReader fileReader = new InputStreamReader(fis,charset);
BufferedReader reader = new BufferedReader(fileReader);
char[] buf = new char[10];
System.out.println("Reading xml file");
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
String content = fileData.toString();
System.out.println("File reading completed");
//split
System.out.println("File Splitting process Started");
int indx = content.indexOf(bodyStart);
String head = content.substring(0, indx - 1);
String body = content.substring(indx);
//write to head file
OutputStreamWriter headFile = new OutputStreamWriter(new FileOutputStream(headName), charset);
headFile.write(head);
System.out.println("New header file created");
//headFile.flush();
headFile.close();
//write body to body file
OutputStreamWriter bodyFile = new OutputStreamWriter(new FileOutputStream(bodyName), charset);
bodyFile.write(body);
System.out.println("New body file created");
bodyFile.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
;
}
}
}
I am not sure how to approach this. Any advice will be much appreciated.