tags:

views:

111

answers:

2

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!

A: 

Hi !

Since you are working with simple and usefull "schema" (as I can see, you have a JSON object and the same data in XML).

If you are usign JavaScript, .NET, PHP, ruby, C++, Java, ... it could be done very easily. For simplier programming, relying on "data schema" is a good thing (if it does not affects performance too much).

You could use a JSON parser to get object into a xml reader/writer...

Whats languague are you coding in ? JSON and XML parsers are available for a lot of PL and can get you working in minutes doing manipulating your data tree.

I hope this help you a bit ... Comment if I'm not clear ...

And please visit www.developerit.com for any information on programming, IT, software and web design.

Developer IT
+4  A: 

what you want is a zipper. see http://stackoverflow.com/questions/2872921/insertions-into-zipper-trees-on-xml-files-in-clojure for some good answers to your question

Jeremy Wall