[ACCEPTED]-How do I create XML using Nokogiri::XML::Builder with a hyphen in the element name?-nokogiri
Accepted answer
Here you go:
require 'nokogiri'
b = Nokogiri::XML::Builder.new do |xml|
xml.send(:"fooo-bar", "hello")
end
puts b.to_xml
0
Bart Vandendriessche's answer works but 4 there is a simpler solution if you only 3 want a text field within the element.
require 'nokogiri'
b = Nokogiri::XML::Builder.new do |xml|
xml.send(:"foo-bar", 'hello')
end
puts b.to_xml
Generates:
<?xml version="1.0"?>
<foo-bar>hello</foo-bar>
If 2 you need them to be nested then you can 1 pass a block
require 'nokogiri'
b = Nokogiri::XML::Builder.new do |xml|
xml.send(:'foo-bar') {
xml.send(:'bar-foo', 'hello')
}
end
puts b.to_xml
Generates:
<?xml version="1.0"?>
<foo-bar>
<bar-foo>hello</bar-foo>
</foo-bar>
Aaron Patterson's answer is correct and 5 will work for element names containing any 4 character that may otherwise be interpreted 3 by the Ruby parser.
Answering Angela's question: to 2 place text inside a element created this 1 way you can do something like this:
require 'rubygems'
require 'nokogiri'
b = Nokogiri::XML::Builder.new do |xml|
xml.send(:'foo.bar') {
xml.text 'hello'
}
end
puts b.to_xml
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.