xom-interest AT lists.ibiblio.org
Subject: XOM API for Processing XML with Java
List archive
[XOM-interest] Appending at the end of an Xml file efficiently
- From: Anvesh Vagiri <yahanvesh AT gmail.com>
- To: xom-interest AT lists.ibiblio.org
- Subject: [XOM-interest] Appending at the end of an Xml file efficiently
- Date: Sat, 20 Jun 2009 12:04:08 +0530
hi,
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.
http://marc.info/?l=xom-interest&m=117286293113470&w=2
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 1Gb...so 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
nodes...so 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
***********************
<ROOT>
<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>
<KEY ID="2">NAXIS = 1 /
Dimensionality </KEY>
<KEY ID="3">NAXIS1 = 0
</KEY>
</EXT>
<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>
<KEY ID="2">NAXIS = 1 /
Dimensionality </KEY>
<KEY ID="3">NAXIS1 = 0
</KEY>
</EXT>
<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>
<KEY ID="2">NAXIS = 1 /
Dimensionality </KEY>
<KEY ID="3">NAXIS1 = 0
</KEY>
</EXT>
</ROOT>
*****************
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
extensions!
**************The code************
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
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.io.StreamingSerializer;
import nux.xom.io.StreamingSerializerFactory;
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 whoschek.AT.lbl.DOT.gov
*/
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 {
allHeaders=Headers;
extno=extNo;
// 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
file.
String xmlfile1=xmlfile.substring(0, xmlfile.lastIndexOf(".")-1);
xmlfile1=xmlfile1+"1"+".xml";
f1=new File(xmlfile1);
if(f1.exists())
{
System.out.println("it already exists but deleting");
f1.delete();
f1.createNewFile();
}
OutputStream out = new FileOutputStream(f1);
StreamingSerializer serializer = new
StreamingSerializerFactory().createXMLSerializer(out, "UTF-8");
NodeFactory streamingNodeFactory
=XOMUtil.getRedirectingNodeFactory(serializer);
NodeFactory appendingNodeFactory = new
AppendingNodeFactory(streamingNodeFactory);
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);
f1.delete();
f2=new File(xmlfile1);
f2.renameTo(f1);
//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");
System.out.println("EXT:"+exxt);
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)));
key.appendChild((String)kew.get(j));
ext.appendChild(key);
}
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)
print(createNodeToAppend(extno,tmp));
extno++;
}
child.makeText("\n\n");
}
return child.finishMakingElement(element);
}
private void print(Element elem) {
child.startMakingElement(elem.getQualifiedName(),elem.getNamespaceURI());
for (int i=0; i < elem.getAttributeCount(); i++) {
Attribute attr = elem.getAttribute(i);
child.makeAttribute(attr.getQualifiedName(),attr.getNamespaceURI(),
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) {
child.makeText(node.getValue());
} else if (node instanceof Comment) {
child.makeComment(node.getValue());
} else if (node instanceof ProcessingInstruction) {
ProcessingInstruction pi =
(ProcessingInstruction) node;
child.makeProcessingInstruction(pi.getTarget(), pi.getValue());
}
}
child.finishMakingElement(elem);
}
// 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) {
child.finishMakingDocument(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);
}
}
}
-
[XOM-interest] Appending at the end of an Xml file efficiently,
Anvesh Vagiri, 06/20/2009
-
Re: [XOM-interest] Appending at the end of an Xml file efficiently,
Elliotte Harold, 06/20/2009
-
Re: [XOM-interest] Appending at the end of an Xml file efficiently,
Michael Ludwig, 06/20/2009
- Re: [XOM-interest] Appending at the end of an Xml file efficiently, Elliotte Harold, 06/21/2009
-
Re: [XOM-interest] Appending at the end of an Xml file efficiently,
Michael Ludwig, 06/20/2009
- Re: [XOM-interest] Appending at the end of an Xml file efficiently, Elliotte Harold, 06/20/2009
- <Possible follow-up(s)>
- Re: [XOM-interest] Appending at the end of an Xml file efficiently, Tatu Saloranta, 06/20/2009
- Re: [XOM-interest] Appending at the end of an Xml file efficiently, Anvesh Vagiri, 06/22/2009
-
Re: [XOM-interest] Appending at the end of an Xml file efficiently,
Elliotte Harold, 06/20/2009
Archive powered by MHonArc 2.6.24.