rubyxmllibxml2libxml-rubyexport-to-xml

Add comment nodes outside root with ruby-libxml


I am writing an xml exporter in ruby and I am using libxml package for it. I want to write some comment nodes outside the root element

<?xml version="1.0" encoding="UTF-8"?>
<!-- comment -->
<root>
 <childnode />
</root>

How do I accomplish export to above format?

Sample ruby code to generate the above (without accounting for comment node)

doc = XML::Document.new()
rootNode = XML::Node.new('root')
doc.root = rootNode
childNode = XML::Node.new('childnode')
childnode << rootNode

Solution

  • ended up editing the xml string manually to add the comments outside the root node (for both libxml and nokogiri