tags:

views:

463

answers:

2

I am loading some data from an XML document, modifying it, and writing back out to XML. The reading is done using a SAX parser library and the writing is done with a piece of custom code. Sometimes, the file is modified externally, and extra elements are added (such as references to stylesheets). Rather than losing these extra elements when I load and save the file, I would like to pass through any unknown tags so that they appear

When unknown elements are separate from interpreted elements, it should be straightforward to save unknown elements and attributes as strings and output these afterwards, but when they are interspersed and nested inside interpreted elements, it becomes less obvious.

Can anybody suggest a succinct way to do this? Would it be simpler to switch to a DOM parser? Performance is not an issue.

NB. I am working in C++ with the Gnome Glib::Markup::Parser, but would prefer language/library agnostic answers.

+1  A: 

I don't know how you've written your content handler, but it's methods should be called on all events, including on the external modifications you want to preserve. Your startElement() and endElement() callbacks could test element name and attributes to decide whether to call specialized methods on the elements to be modified, but by default just reconstruct and output the event on which the callback was called. In that way, any elements you don't specifically handle specially just get output by default.

You could also do the same kind of thing in XSLT. Take an identity transform (a stylesheet that outputs exactly what it is given for input) and add to it templates with more specific match expressions for the custom modifications. I find XSLT easier to work with for most applications than either SAX or DOM.

ChuckB
A: 

I believe SAX is not the right way to go when you want to modify an xml file and save it back to some other file after being altered. My advice is to use DOM. It will load the entire file (including the modifications done externally) so that you just have to think about what you want to do. Everything will be back in place when you save it afterwards.

Benoît