Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add XML string to Nokogiri Builder

Tags:

xml

ruby

nokogiri

I have an existing Nokogiri builder and some xml nodes in a string from a different source. How can I add this string to my builder?

str = "<options><cc>true</cc></options>"
xml = Nokogiri::XML::Builder.new do |q|
  q.query do |f|
    f.name "awesome"
    f.filter str
  end
end

This escapes str into something like:

xml.to_xml
=> "<?xml version=\"1.0\"?>\n<query>\n  <name>awesome</name>\n  <filter>&lt;options&gt;&lt;cc&gt;true&lt;/cc&gt;&lt;/options&gt;</filter>\n</query>\n"

I have found many, many similar things, including nesting builders and using the << operator, but nothing works to insert a full xml node tree into a builder block. How can I make that string into real nodes?

like image 471
genkilabs Avatar asked Dec 27 '12 22:12

genkilabs


1 Answers

What problems did you find using <<? This works for me:

xml = Nokogiri::XML::Builder.new do |q|
  q.query do |f|
    f.name "awesome"
    f << str
  end
end

and avoids using the private insert method.

like image 160
matt Avatar answered Sep 28 '22 10:09

matt