Мой исходный XML-файл выглядит следующим образом
<?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>
Я разделяю этот файл и создаю 2 файла.Одним из них является файл .header, а другим - файл .body.Файл тела будет начинаться с тега «Example_Data».Теперь проблема в том, что при создании файла .body контент создается с самого начала файла без учета пробелов.Например:
<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>
Но я хочу учесть и пробелы, чтобы содержимое файла тела начиналось с позиции, которую оно занимает в исходном файле (после 4 пробелов или любого количества пробелов перед тегом Example_DataЯ могу жестко запрограммировать 4 пробела, но это не поможет моему делу, потому что есть другие файлы, где перед этим тегом может быть больше пробелов.
Вот фрагмент кода, над которым я работаюрасщепление:
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 {
;
}
}
}
Я не уверен, как подойти к этому.Любой совет будет высоко ценится.