How to: XML parse then modify


#1
require "xml"

xml = <<-XML
 <person>
  <firstname>Jane</firstname>
  <lastname>Doe</lastname>
 </person>

d = XML.parse(xml)

I can change firstname with:

d.xpath_node("person/firstname/text()").as(XML::Node).text = "May"

but I can’t figure how to add address.


#2

The current XML API is mostly oriented towards reading/parsing XMLs, not so much about modifying them. There is a big chunk of the API that’s missing. PRs are welcome.


#3

You can check out TAX (Tree API for XML) I wrote a while back https://github.com/vonKingsley/tax. be careful though because you can write invalid XML.