Appending and prepending to XML files with Clojure

Posted by Isaac Copper on Stack Overflow See other posts from Stack Overflow or by Isaac Copper
Published on 2010-05-30T02:50:02Z Indexed on 2010/05/30 3:02 UTC
Read the original article Hit count: 342

Filed under:
|

I have an XML file with format similar to:

<root>
   <baby>
      <a>stuff</a>
      <b>stuff</b>
      <c>stuff</c>
   </baby>
       ...
   <baby>
      <a>stuff</a>
      <b>stuff</b>
      <c>stuff</c>
   </baby>
</root>

And a Clojure hash-map similar to:

{:a "More stuff" :b "Some other stuff" :c "Yet more of that stuff"}

And I'd like to prepend XML (¶) created from this hash-map after the <root> tag and before the first <baby>

(¶) The XML to prepend would be like:

   <baby>
      <a>More stuff</a>
      <b>Some other stuff</b>
      <c>Yet more of that stuff</c>
   </baby>

I'd also like to be able to delete the last one (or n...) <baby>...</baby>s from the file.

I'm struggling with coming up with an idiomatic was to prepend and append this data. I can do raw string manipulations, or parse the XML using xml/parse and xml-seq and then roll through the nodes and (somehow?) replace the data there, but that seems messy.

Any tips? Ideas? Hints? Pointers? They'd all be much appreciated.

Thank you!

© Stack Overflow or respective owner

Related posts about Xml

Related posts about clojure