3

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!

Isaac
  • 15,783
  • 9
  • 53
  • 76
  • 2
    Isaac, I don't have time to write a detailed reply right now but since you already know Enlive basics, see http://stackoverflow.com/questions/2872921/insertions-into-zipper-trees-on-xml-files-in-clojure/2876510#2876510 – cgrand May 30 '10 at 07:56

1 Answers1

4

what you want is a zipper. see Insertions into Zipper trees on XML files in Clojure for some good answers to your question

Community
  • 1
  • 1
Jeremy Wall
  • 23,907
  • 5
  • 55
  • 73