4

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
Schu
  • 1,124
  • 3
  • 11
  • 23
  • Only operation i can do on the documnet object is setting its root – Schu Aug 25 '11 at 18:29
  • However, I can create comment nodes and add them under the root node – Schu Aug 25 '11 at 18:30
  • Nokogiri lets you do that. Nokogiri is based on libxml2 too and is a better wrapper than ruby-libxml. – Serabe Aug 27 '11 at 11:50
  • i tried nokogiri as well. i use builder to export. so within the builder code, we have some how create the comment node. Based on the nokogiri API, i need to get hold of "Document" from the builder object to add a comment, which I wasnt able to figure out. – Schu Aug 27 '11 at 15:04
  • 1
    Take a look at last part [here](http://nokogiri.org/Nokogiri/XML/Builder.html) – Serabe Aug 27 '11 at 17:32
  • i remember ruby throwing a null reference for xml.doc access (xml is anonymous variable), not too sure though. i will check again after the weekend. tyvm – Schu Aug 27 '11 at 23:00

2 Answers2

0

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

Schu
  • 1,124
  • 3
  • 11
  • 23
-2
<?xml version="1.0" encoding="UTF-8" ?>
<List type = "" =”00:75:00” =”00:00:05”>

</List>

Yes

<?xml version="1.0" encoding="UTF-8" ?>
<List type = "update" >
</List>
osdevkid
  • 137
  • 2
  • 3
  • 14