I am trying to build XML using Nokogiri with some tags that have both attributes and plain text inside the tag. So I am trying to get to this:
There are two approaches you can use.
Using .text
You can call the .text
method to set the text of a node:
builder = Nokogiri::XML::Builder.new { |xml|
xml.Transaction("requestName" => "OrderRequest") do
xml.Option("b" => "hive"){ xml.text("hello") }
end
}
which produces:
Solution using text parameter
Alternatively, you can pass the text in as a parameter. The text should be passed in before the attribute values. In other words, the tag is added in the form:
tag "text", :attribute => 'value'
In this case, the desired builder would be:
builder = Nokogiri::XML::Builder.new { |xml|
xml.Transaction("requestName" => "OrderRequest") do
xml.Option("hello", "b" => "hive")
end
}
Produces the same XML: