我该如何将一个
我的目前解决方案是序列化该元素并使用
Nokogiri::XML::Element
添加到使用 Nokogiri::XML::Buider
创建的 XML 文档中?我的目前解决方案是序列化该元素并使用
<<
方法让 Builder 重新解释它。orig_doc = Nokogiri::XML('<root xmlns="foobar"><a>test</a></root>')
node = orig_doc.at('/*/*[1]')
puts Nokogiri::XML::Builder.new do |doc|
doc.another {
# FIXME: this is the round-trip I would like to avoid
xml_text = node.to_xml(:skip_instruct => true).to_s
doc << xml_text
doc.second("hi")
}
end.to_xml
# The expected result is
#
# <another>
# <a xmlns="foobar">test</a>
# <second>hi</second>
# </another>
然而Nokogiri::XML::Element
是一个相当大的节点(在千字节和数千个节点的级别),而这段代码位于热路径中。分析显示序列化/解析往返非常昂贵。
我该如何指示Nokogiri Builder将现有的XML元素node
添加到“当前”位置?
#insert
搞的要好得多。 - gioele