I've made a program that can read an xml file in and print the values from it back out. The xml file it handles is very large with like 40,000 entries of different data, and so I made a generic TreeNode class to handle each of those entries. However, as my program handles each entry of data by making a TreeNode object, it starts to consume a lot of RAM space until it runs out of space before it reaches the end. I know you can recycle your data structure, but I've looked online to find out how you can go about doing so, and as of yet I've not found a way of doing so. I know you can call recycle() on objects but for some reason it doesn't seem to work on my TreeNode. I was wondering whether anyone knew how I could go about implementing recycle in my code to help me overcome this issue, or any other way in which I can overcome my problem.
My code is below (I know it's not the best and most efficient way of doing it but it works):-
public class XML {
TreeNode root;
public XML(){
try{
InputStream file = new FileInputStream("/Users/Kevlar/Dropbox/PhD/Java/Metabolites/testOutput.xml");
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader reader = inputFactory.createXMLStreamReader(file);
reader.nextTag();
do{
if(reader.getEventType() == XMLStreamConstants.START_ELEMENT && reader.getLocalName() == "metabolite"){
printChildren(mainElements(reader).children());
}
else{
reader.next();
}
} while(reader.hasNext());
} catch (XMLStreamException e){
System.err.println("XMLStreamException : " + e.getMessage());
} catch (FactoryConfigurationError e){
System.err.println("FactoryConfigurationError : " + e.getMessage());
} catch (FileNotFoundException e){
System.err.println("FileNotFoundException : " + e.getMessage());
}
}
private TreeNode mainElements(XMLStreamReader reader) throws XMLStreamException {
Element rootElement = new Element();
rootElement.setName(reader.getLocalName());
System.out.println(rootElement.getName());
root = new TreeNode(rootElement);
int level = 1;
do{
int event = reader.next();
if(event == XMLStreamConstants.START_ELEMENT) {
Element element = new Element();
element.setName(reader.getLocalName());
TreeNode node = new TreeNode(element);
level++;
if(level == 2){
root.add(subElements(reader, node));
level--;
}
} else if(event == XMLStreamConstants.END_ELEMENT){
level--;
}
} while(level > 0);
return root;
}
private TreeNode subElements(XMLStreamReader reader, TreeNode node) throws XMLStreamException {
int level = 1;
do{
int event = reader.next();
if(event == XMLStreamConstants.START_ELEMENT) {
Element subElement = new Element();
subElement.setName(reader.getLocalName());
TreeNode subNode = new TreeNode(subElement);
level++;
if(level == 2){
node.add(subElements(reader, subNode));
level--;
}
} else if(event == XMLStreamConstants.CHARACTERS && !reader.isWhiteSpace()){
node.getElement().setValue(reader.getText());
} else if(event == XMLStreamConstants.END_ELEMENT){
level--;
}
} while(level > 0);
return node;
}
private void printChildren(TreeNode[] children) {
for(int i = 0; i < children.length; i++){
TreeNode node = children[i];
if(node.hasChildren()){
System.out.println("Element:- " + node.getElementName());
printChildren(node.children());
}
else if(!node.hasChildren()){
System.out.println("Element:- " + node.getElementName());
System.out.println("Value:- " + node.getElementValue());
}
}
}
public static void main(String[] args) throws XMLStreamException{
XML test = new XML();
}
}