Using nokogiri to generate XML, but my XML tags are all html-encoded :(

839 views
Skip to first unread message

swese44

unread,
Nov 10, 2010, 12:34:15 AM11/10/10
to nokogiri-talk
I'm sure this is a simple fix, but I'm stuck scratching my head. I'm
using an example posted here:
http://kete.lighthouseapp.com/projects/14288/tickets/177-tags-list-rss-feed-increasingly-slower

I'm on Rails 3.0.1 and Ruby 1.9.2, Nokogiri 1.4.3.1. Maybe I'm just
following an older example and things have changed for Rails 3? Here's
the code in my photos.xml.erb file:

<%= Nokogiri::XML::Builder.new(:encoding => 'UTF-8') { |xml|
xml.photos {
for photo in @photos
xml.photo {
xml.id photo[:id]
xml.title photo[:title]
xml.description { xml.cdata photo[:description] }
xml.created_at photo[:created_at]
xml.file_file_name photo[:file_file_name]
xml.file_file_size photo[:file_file_size]
xml.has_adult_content photo[:has_adult_content]
xml.is_altered photo[:is_altered]
xml.is_popular photo[:is_popular]
xml.lat photo[:lat]
xml.long photo[:long]
xml.num_downloads photo[:num_downloads]
xml.num_likes photo[:num_likes]
xml.tags {
for tag in photo.tags
xml.tag {
xml.id tag[:id]
xml.name tag[:name]
}
end
}
}
end
}
}.to_xml
%>




And this is what it generates, all html-encoded characters instead of
XML tags:


&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;photos&gt;
&lt;photo&gt;
&lt;id&gt;3&lt;/id&gt;
&lt;title&gt;tsetset&lt;/title&gt;
&lt;description&gt;&lt;![CDATA[testing]]&gt;&lt;/description&gt;
&lt;created_at&gt;2010-11-08 07:07:34 UTC&lt;/created_at&gt;
&lt;file_file_name&gt;00462075a788ec72822bb26dc62320ef.jpg&lt;/
file_file_name&gt;
&lt;file_file_size&gt;142683&lt;/file_file_size&gt;
&lt;has_adult_content&gt;false&lt;/has_adult_content&gt;
&lt;is_altered&gt;true&lt;/is_altered&gt;
&lt;is_popular&gt;&lt;/is_popular&gt;
&lt;lat&gt;&lt;/lat&gt;
&lt;long&gt;&lt;/long&gt;
&lt;num_downloads&gt;&lt;/num_downloads&gt;
&lt;num_likes&gt;&lt;/num_likes&gt;
&lt;tags&gt;
&lt;tag&gt;
&lt;id&gt;1&lt;/id&gt;
&lt;name&gt;asdf&lt;/name&gt;
&lt;/tag&gt;
&lt;tag&gt;
&lt;id&gt;4&lt;/id&gt;
&lt;name&gt;ass face&lt;/name&gt;
&lt;/tag&gt;
&lt;tag&gt;
&lt;id&gt;5&lt;/id&gt;
&lt;name&gt;black&lt;/name&gt;
&lt;/tag&gt;
&lt;tag&gt;
&lt;id&gt;6&lt;/id&gt;
&lt;name&gt;chargers&lt;/name&gt;
&lt;/tag&gt;
&lt;tag&gt;
&lt;id&gt;7&lt;/id&gt;
&lt;name&gt;poppy pants&lt;/name&gt;
&lt;/tag&gt;
&lt;/tags&gt;
&lt;/photo&gt;
&lt;/photos&gt;


Any ideas? Thanks!

Aaron Patterson

unread,
Nov 10, 2010, 12:40:01 AM11/10/10
to nokogi...@googlegroups.com
Hi,

You need to tell Rails that your XML is html safe. Try "to_xml.html_safe!"

Hope that helps.

--
Aaron Patterson
http://tenderlovemaking.com/

swese44

unread,
Nov 10, 2010, 1:04:20 AM11/10/10
to nokogiri-talk
Thanks Aaron. Unfortunately Rails throws this error now:
<code>You can't call html_safe! on a String</code>




On Nov 9, 9:40 pm, Aaron Patterson <aaron.patter...@gmail.com> wrote:
> Hi,
>
>
>
>
>
>
>
>
>
> On Tue, Nov 9, 2010 at 9:34 PM, swese44 <swes...@gmail.com> wrote:
> > I'm sure this is a simple fix, but I'm stuck scratching my head. I'm
> > using an example posted here:
> >http://kete.lighthouseapp.com/projects/14288/tickets/177-tags-list-rs...

Aaron Patterson

unread,
Nov 10, 2010, 1:07:57 AM11/10/10
to nokogi...@googlegroups.com
On Tue, Nov 9, 2010 at 10:04 PM, swese44 <swe...@gmail.com> wrote:
> Thanks Aaron. Unfortunately Rails throws this error now:
> <code>You can't call html_safe! on a String</code>

Try just "html_safe". I can never remember. :-(

swese44

unread,
Nov 10, 2010, 1:21:13 AM11/10/10
to nokogiri-talk
Awesome, that did the trick. One last question, is there a special
format to display both a node attribute and text within the same node?
I haven't seen an example of that anywhere. I've tried a few different
ways but I can't seem to figure it out, once I add the attribute then
the text is no longer displayed.

xml.is_altered(:type => 'boolean') photo[:is_altered] # Syntax
Error
xml.is_altered(:type => 'boolean') { photo[:is_altered] } # Empty
node with attribute

Thanks again!
P.J.


On Nov 9, 10:07 pm, Aaron Patterson <aaron.patter...@gmail.com> wrote:

swese44

unread,
Nov 10, 2010, 1:24:17 AM11/10/10
to nokogiri-talk
Ah I got it! Use xml.text as if it were xml.cdata.

Thanks again,
P.J.
Reply all
Reply to author
Forward
0 new messages