使用 Nokogiri 如何将 Node 对象内容插入到 XML::Builder 结构中?

foo*_*mes 2 ruby xml nokogiri

使用 Nokogiri 如何将 Node 对象内容插入到 XML::Builder 结构中?

#source nodes
mynodes = [...array of Nodes...]

#where I want to dump source nodes
target_for_nodes = somebuilder.doc.xpath('//mydoc/mynodecollection').first

#drop the nodes into place
Nokogiri::XML::Builder.with(target_for_nodes) do |xml|
  mynodes.each do |node|
     xml.text node.to_xml  #gives escaped text- how to drop real XML here from the Node?
  end
end
Run Code Online (Sandbox Code Playgroud)

它提供了转义文本,但我不清楚如何从 Node 对象中删除真正的 XML?

foo*_*mes 5

嗯。看来我只需要使用

xml << node.to_xml 
Run Code Online (Sandbox Code Playgroud)

而不是

xml.text node.to_xml
Run Code Online (Sandbox Code Playgroud)

干杯!

  • 当人们阅读文档时会发现什么是完全令人惊奇的。 (2认同)