Re: [xml] xmlWriter?



On Mon, 2003-05-12 at 10:32, Stefan Seefeld wrote: 
Jeroen Cranendonk wrote:

The only problem I have now is that libxml2 only seems to support writing
files from dom tree's ?
which holds the same problem that dom is rather memory consuptive. I'd like
to use a sax interface, or something like the msxml counterpart to the
xmlreader, the xmlwriter. Is there any support in libxml2 to let me do this?
any ideas on how to work around this problem if there isn't ?
Anyone feels like spending some time on implementing this ? ;)

what should that 'xmlwriter' API do that you can't do yourself by simply
calling printf ?

It'd be nice to have the library handle all the
formatting/escaping/etc.  I.e., call a function like
xml_write_attribute(name, value) and let it deal with escaping quotes in
value and whatnot.

It's a pain to have to write all this in every app that needs to spit
out XML, not to mention it can be error-prone, especially for people not
familiar with all the intracacies of the XML spec.

Writer API could very simple.  (Haven't seen the MS .net writer
interface.)  A very small handful of functions could easily meet all
needs:

xml_write_open(path);
xml_write_begin_element(doc, name);
xml_write_attribute(doc, name, value);
xml_write_cdata(doc, data);
xml_write_escape(doc, data);
xml_write_comment(doc, value);
xml_write_directive(doc, name, value);
xml_write_end_element(doc);
xml_write_close(doc);
(And any others I might've forgetton - XML is too complex ;-)

A quick document could easily be written with something like:

XMLWriter* writer = xml_write_open("out.xml");
xml_write_comment(writer, "Generated by example.c");
xml_write_begin_element(writer, "root");
xml_write_begin_element(writer, "author");
xml_write_cdata(writer, "Sean Middleditch & The Red Llama");
xml_write_end_element(writer); /* author */
xml_write_begin_element(writer, "version");
xml_write_attribute(writer, "major", "1");
xml_write_attribute(writer, "minor", "0");
xml_write_end_element(writer); /* version */
xml_write_end_element(writer); /* root */
xml_writer_close(writer);

That would output something along the lines of the following (minus the
pretty-printing; using it here for readability):
<?xml version="1.0?>
<!-- Generated by example.c -->
<root>
  <author>Sean Middleditch &amp; The Red Llama</author>
  <version major="1" minor="0" />
</root>

Notice how things like the auto-closing of the version element (using
<version .. /> versus <version></version>) as no cdata sections were
written, or how the & was escaped in the author field automagically. 
It's little things like this that make the API easy to use, and also
help avoid mistakes from coders who otherwise must be forced to code
their own XML writers interfaces for every application.

It's reasons like that for which we _have_ libraries, after all.  ;-)

Stefan

_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]