Appending to an XML document

Ross Bleakney rossb at wrq.com
Tue Dec 7 23:08:38 GMT 1999


My apologies if you have already read this in the XSL list. I (and
apparently several other people) have a need to append an element onto an
existing XML file. I would like to avoid reading in the whole document and
then writing it back out again. My original plan was to open the file, find
the end of it, back up a bit to find the last tag, write the new element and
then rewrite the closing tag. I am looking for a generic solution.

I know of no XML API that allows for modifying a document. They make it easy
to create new documents out of old ones, but they don't allow you to modify
an existing file. Doing so would mean the possibility of optimization that
would greatly reduce disk I/O. For example, if you had XML like this:

<Events>
   <Event>...<Event>
   <Event>...<Event>
</Events>

It would be really nice to write code like this:

	ModifyXML modXML = new ModifyXML("MyDoc.XML");	
	Element event = modXML.createElement("Event");
	event.appendChild(modXML.createTextNode("A big event happened"));
	modXML.appendChild("Events", event);
	modXML.update();

An implementor of this interface could take advantage of the fact that
<Events> is the main tag and perform the same sort of work I suggested
(backing up from the end and then writing). The routines for this interface
would be very limited since this would only be used when you want to modify
a document and you know that using SAX (or DOM) is inefficient. Thus there
would be no reason to have an "insertBefore". The API could be limited to
appending and deleting. 

Is there something like this already?

Thanks,
Ross

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To unsubscribe, mailto:majordomo at ic.ac.uk the following message;
unsubscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)





More information about the Xml-dev mailing list