There is a huge xml file(3-4GB) (360000 lines of records) and have to read each line and append each line using Stringbuilder.once it is read it will be processed further. But will not be able to store in the internal memory as the stringbuilder buffer size exceeds. How to split the records and rest before the buffer size exceeds. Kindly suggest.
try {
File file = new File("test.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String builder stringBuilder = new Stringbuilder ();
String line;
int count =0;
while ((line = bufferedReader.readLine()) != null)`enter code here`
{
if (line.startswith("<customer>") ){
stringBuilder .append(line);
}
count++;
}
fileReader.close();
System.out.println(stringBuilder .toString());
} catch (IOException e) {
e.printStackTrace();
}
EDIT: Asker tried with StAX
while (xmlEventReader.hasNext()) {
XMLEvent xmlEvent = null;
try {
xmlEvent = xmlEventReader.nextEvent();
} catch (Exception e) {
e.printStackTrace();
}
if (xmlEvent.isStartElement()) {
StartElement elem = (StartElement) xmlEvent;
if (elem.getName().getLocalPart().equals("<Customer>")) {
if (customerRecord) {
insideChildRecord = true;
}
customerRecord = true;
}
}
if (customerRecord) {
xmlEventWriter.add(xmlEvent);
}
if (xmlEvent.isEndElement()) {
EndElement elem = (EndElement) xmlEvent;
if (elem.getName().getLocalPart().equals("<Customer>")) {
if (insideChildRecord) {
insideChildRecord = false;
} else {
customerRecord = false;
xmlEventWriter.flush();
String cmlChunk = stringWriter.toString()