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><options><cc>true</cc></options></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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With