Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate XML with Nokogiri without "<?xml version=..."? [duplicate]

Tags:

xml

ruby

nokogiri

Possible Duplicate:
Print an XML document without the XML header line at the top

I have a problem with Nokogiri::XML::Builder. I am generating XML wth this code:

builder = Nokogiri::XML::Builder.new do 
    request {
        data '1'
    }
end

And the result is:

<?xml version="1.0" encoding="UTF-8"?><request><data>1</data></request>

How can I remove:

<?xml version="1.0" encoding="UTF-8"?>

from my XML?

like image 998
mibon Avatar asked Jan 17 '12 13:01

mibon


2 Answers

Maybe take just the root node of the current Document object being built – .doc – instead of the whole document?

builder.doc.root.to_s
like image 52
Christopher Creutzig Avatar answered Nov 13 '22 04:11

Christopher Creutzig


A quick and dirty answer is to tell Nokogiri to reparse the resulting output, then look at the root:

require 'nokogiri'

builder = Nokogiri::XML::Builder.new do 
  request {
    data '1'
  }
end

puts Nokogiri::XML(builder.to_xml).root.to_xml

Which outputs:

<request>
  <data>1</data>
</request>
like image 5
the Tin Man Avatar answered Nov 13 '22 03:11

the Tin Man