How to: XML parse then modify

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.

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.

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.