在 Nokogiri 中的特定 XML 标签后插入文本 [英] Insert Text After Specific XML Tag in Nokogiri
本文介绍了在 Nokogiri 中的特定 XML 标签后插入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建以下 XML:
I'd like to create the following XML:
<?xml version="1.0">
<foo>
<bar/>
TEXT GOES HERE
</foo>
使用 Nokogiri 构建结构非常简单:
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
我不知道如何在 <foo>
中插入 TEXT GOES HERE
字符串,但 after <bar/>
.
What I can't figure out is how to insert the TEXT GOES HERE
string inside <foo>
but after <bar/>
.
显然,xml.foo("TEXT GOES HERE")
在
之前生成文本.我错过了什么?
Obviously, xml.foo("TEXT GOES HERE")
produces the text before <bar>
. What am I missing?
推荐答案
您想要 text
方法:
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>
这篇关于在 Nokogiri 中的特定 XML 标签后插入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文