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!
what you want is a zipper. see Insertions into Zipper trees on XML files in Clojure for some good answers to your question