Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] Appending at the end of an Xml file efficiently

xom-interest AT

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Anvesh Vagiri <yahanvesh AT>
  • To: xom-interest AT
  • Subject: [XOM-interest] Appending at the end of an Xml file efficiently
  • Date: Sat, 20 Jun 2009 12:04:08 +0530


I am just a newbie to XOM, and i need to append nodes to an existing
xml file always at the end
I used the code available here.

But unfortunately it gives Out of memory error in my java netbeans
when the file size increases more than just 10-15MB.

There is no limit to the size of xml file in my case.. it may even
reach to please suggest efficient methods as well as less
time consuming operations as well.

and i also read that if efficency really matters its better to which
to other things like database Btrees, etc.

But should it run out of memory for even files of such small sizes ?

Im actually suppose to append to the same file , and i had append many here is how i changed the code

Ill be grateful if some one can just tell me if i am doing any mistakes?

The concept in the code ive got here tht it is streaming...but would
it make any difference if i have to always append at the end.

The sample xml file
<EXT ID="0">
<KEY ID="0">SIMPLE = T / Java
FITS: Mon Feb 11 10:25:35 GMT 2008 </KEY>
<KEY ID="1">BITPIX = 32
<KEY ID="2">NAXIS = 1 /
Dimensionality </KEY>
<KEY ID="3">NAXIS1 = 0
<EXT ID="1">
<KEY ID="0">SIMPLE = T / Java
FITS: Mon Feb 11 10:25:35 GMT 2008 </KEY>
<KEY ID="1">BITPIX = 32
<KEY ID="2">NAXIS = 1 /
Dimensionality </KEY>
<KEY ID="3">NAXIS1 = 0
<EXT ID="2">
<KEY ID="0">SIMPLE = T / Java
FITS: Mon Feb 11 10:25:35 GMT 2008 </KEY>
<KEY ID="1">BITPIX = 32
<KEY ID="2">NAXIS = 1 /
Dimensionality </KEY>
<KEY ID="3">NAXIS1 = 0
This is just a sample xml file so in a similar way there is no limit
to the number of keys in each extension and no limit to the number of

**************The code************


import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Comment;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Node;
import nu.xom.NodeFactory;
import nu.xom.Nodes;
import nu.xom.ProcessingInstruction;
import nu.xom.Text;
import nux.xom.pool.XOMUtil;

import java.util.Vector;

* Efficiently appends an element to the end of an XML file, in a
streaming manner.
* @author
public class AppendXml {
static File f1,f2;
static Vector allHeaders;

static int extno;
public static void Append(String xmlfile,Vector Headers,int extNo)
throws Exception {


// I am creating a new file in whcih i write the old +appended data
and then delete the old file and rename thenew file name again to old
String xmlfile1=xmlfile.substring(0, xmlfile.lastIndexOf(".")-1);
f1=new File(xmlfile1);
System.out.println("it already exists but deleting");

OutputStream out = new FileOutputStream(f1);
StreamingSerializer serializer = new
StreamingSerializerFactory().createXMLSerializer(out, "UTF-8");

NodeFactory streamingNodeFactory
NodeFactory appendingNodeFactory = new

start1 = System.currentTimeMillis();
System.out.println("just started builidng");
new Builder(appendingNodeFactory).build(new File(xmlfile));
//stream through
end1 = System.currentTimeMillis();
System.out.println("for building "+(end1-start1)+" ms.");
// check results:
//System.out.println("THe new appended file "+new
Builder().build(new File(xmlfile1)).toXML());
f1=new File(xmlfile);
f2=new File(xmlfile1);
//System.out.println("fe "+f2.getAbsoluteFile());

private static final class AppendingNodeFactory extends NodeFactory {

private final NodeFactory child;

public AppendingNodeFactory(NodeFactory child) {
this.child = child;

// replace this with whatever tree you want to see appended...
private Element createNodeToAppend(int exxt,Vector kew) {

Element ext = new Element("EXT");
ext.addAttribute(new Attribute("ID", String.valueOf(exxt)));
for(int j=0;j<kew.size();j++)
Element key = new Element("KEY");
key.addAttribute(new Attribute("ID", String.valueOf(j)));
return ext;

public Nodes finishMakingElement(Element element) {
if (element.getParent() instanceof Document) {

for(int g=0;g<allHeaders.size();g++)
Vector tmp=(Vector)allHeaders.get(g);
//THe vector containing a set of KEY values for
each Extension denoted by extno(Extension number)
return child.finishMakingElement(element);

private void print(Element elem) {

for (int i=0; i < elem.getAttributeCount(); i++) {
Attribute attr = elem.getAttribute(i);

attr.getValue(), attr.getType());
for (int i=0; i < elem.getChildCount(); i++) {
Node node = elem.getChild(i);
if (node instanceof Element) {
print((Element)node); // recurse
} else if (node instanceof Text) {
} else if (node instanceof Comment) {
} else if (node instanceof ProcessingInstruction) {
ProcessingInstruction pi =
(ProcessingInstruction) node;

child.makeProcessingInstruction(pi.getTarget(), pi.getValue());

// remaining methods simply delegate through to child...
public Element makeRootElement(String name, String namespace) {
return child.makeRootElement(name, namespace);

public Element startMakingElement(String name, String namespace) {
return child.startMakingElement(name, namespace);

public Document startMakingDocument() {
return child.startMakingDocument();

public void finishMakingDocument(Document document) {

public Nodes makeAttribute(String name, String URI, String
value, Attribute.Type type) {
return child.makeAttribute(name, URI, value, type);

public Nodes makeComment(String data) {
return child.makeComment(data);

public Nodes makeDocType(String rootElementName, String
publicID, String systemID) {
return child.makeDocType(rootElementName, publicID, systemID);

public Nodes makeText(String data) {
return child.makeText(data);

public Nodes makeProcessingInstruction(String target, String data)
return child.makeProcessingInstruction(target, data);


Archive powered by MHonArc 2.6.24.

Top of Page