<p>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 </p>

<p>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. </p>

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

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

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.