问题
I'd like to create the following XML:
<?xml version="1.0">
<foo>
<bar/>
TEXT GOES HERE
</foo>
The structure is pretty simple to build with Nokogiri:
builder = Nokogiri::XML::Builder.new do |xml|
xml.foo {
xml.bar {}
}
end
puts builder.to_xml
What I can't figure out is how to insert the TEXT GOES HERE
string inside <foo>
but after <bar/>
.
Obviously, xml.foo("TEXT GOES HERE")
produces the text before <bar>
. What am I missing?
回答1:
You want the text method:
require 'nokogiri'
builder = Nokogiri::XML::Builder.new do |xml|
xml.foo {
xml.bar
xml.text "TEXT GOES HERE"
}
end
puts builder.doc
#=> <?xml version="1.0"?>
#=> <foo><bar/>TEXT GOES HERE</foo>
来源:https://stackoverflow.com/questions/10147762/insert-text-after-specific-xml-tag-in-nokogiri