Append elements using Nokogiri::XML::Builder

纵然是瞬间 提交于 2019-12-05 09:39:31

Your code produces the following XML, which seems to meet your specifications. It doesn't produce an empty oneChild, at any rate. If this isn't what you're looking for, can you tell us what your ideal output would be?:

builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
  xml.myRoot do |xml|
    xml.oneChild
    xml.anotherChild
  end
end

puts builder.to_xml

# <?xml version="1.0" encoding="UTF-8"?>
# <myRoot>
#   <oneChild/>
#   <anotherChild/>
# </myRoot>   

node = builder.doc.xpath('//myRoot/oneChild').first
Nokogiri::XML::Builder.with(node) do |xml|
  xml.childOfOneChild 'Im a child of oneChild'
end

puts builder.to_xml

# <?xml version="1.0" encoding="UTF-8"?>
# <myRoot>
#   <oneChild>
#     <childOfOneChild>Im a child of oneChild</childOfOneChild>
#   </oneChild>
#   <anotherChild/>
# </myRoot>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!