3

How can I, if possible at all, programmatically edit xml data in Emacs Lisp?

What I need to do, is to programmatically add new nodes into arbitrary locations in the xml:

<root>
  <child>
  </child>
</root>

(xml-add-element xml "hello" (xml-element-by-tagname xml "child"))

<root>
  <child>
    <hello></hello>
  </child>
</root>

Edit: I got as far as parsing the xml into a lisp structure and editing it with xml.el and dom.el, but now I need to turn the lisp structure back into xml data.

Gerstmann
  • 5,368
  • 6
  • 37
  • 57
  • 1
    Funny I ran into exact same issue today. Trey Jackson's code from this question http://stackoverflow.com/questions/797442/is-there-an-emacs-lisp-library-for-generating-html got me pretty close. But it's not 100% what I needed because if the sexp's contain any whitespace nodes, Trey's solution eats the whitespace. I'm very close to my own implementation of xmlgen that should output the xml exactly the way it was before parsed by xml-parse-* functions. I'll post as an answer as soon as it's complete. – Upgradingdave Sep 08 '12 at 10:33

1 Answers1

5

here's my version for converting s-expressions created from xml-parse* functions back into xml strings. Hope it's helpful!

https://github.com/upgradingdave/xml-to-string

Upgradingdave
  • 12,916
  • 10
  • 62
  • 72