Reputation: 977
I need to form such xml:
<jobs>
<job>
<title><![CDATA[cleaner]]></title>
<description><![CDATA[cleaner in af]]></description>
<text><![CDATA[cleaner weekly in af]]></text>
<referencenumber><![CDATA[518]]></referencenumber>
<company><![CDATA[we q.]]></company>
<country_code><![CDATA[NL]]></country_code>
<city><![CDATA[af]]></city>
<url><![CDATA[url]]></url>
</job>
</jobs>
builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
xml.jobs {
data.each do |data|
xml.job {
xml.title {
xml.cdata "..."
}
xml.text {
xml.cdata "..."
}
end
}
end
The above isn't working because text
is an existing method on builder.
How do I create a <text>...</text>
node?
Upvotes: 3
Views: 323
Reputation: 114138
From the docs:
The builder works by taking advantage of method_missing. Unfortunately some methods are defined in ruby that are difficult or dangerous to remove. You may want to create tags with the name “type”, “class”, and “id” for example. In that case, you can use an underscore to disambiguate your tag name from the method call.
Appending an underscore also works for “text”, i.e. use text_
instead:
builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
xml.job {
xml.text_ {
xml.cdata 'foo bar baz'
}
}
end
puts builder.to_xml
Output:
<?xml version="1.0" encoding="UTF-8"?>
<job>
<text><![CDATA[foo bar baz]]></text>
</job>
Upvotes: 3