xml - How to change Rails 3 to_xml encoding -


im using rails 3 to_xml on model few options include, except , methods. not first time using to_xml.

i'm doing this:

to_xml(include: {   order: {     methods: [:my_avg],     except:  [:this_attr, :and_this_attr ]   },   customer: {} }) 

the xml result:

<?xml version="1.0" encoding="utf-8"?> <my-model>   <attr1 type="integer">12</attr1>   <attr2 type="integer">12</attr2>   <order>     <name>foo</name>     <desc>bar</desc>     <my-avg>       <avg type="integer">123</avg>       <foo>ok</foo>     </my-avg>   </order>   <updated-at type="datetime">2014-04-14t11:16:56-03:00</updated-at> </my-model> 

but want change xml encoding iso_8859_1 instead of utf8.

i haven't seen encoding option on activerecord::serialization module.

if add 1 encoding option creates xml attribute instead of changing encoding results on xml:

<?xml version="1.0" encoding="utf-8"?> <my-model>   <attr1 type="integer" encoding="iso-8859-1">12</attr1>   <attr2 type="integer" encoding="iso-8859-1">12</attr2>   <order>     <name>foo</name>     <desc>bar</desc>     <my-avg>       <avg type="integer">123</avg>       <foo>ok</foo>     </my-avg>   </order>   <updated-at type="datetime">2014-04-14t11:16:56-03:00</updated-at> </my-model> 

is there way specify encoding using activerecord's to_xml?

you may override to_xml in model & specify encoding. work:

class modelname < activerecord::base   def to_xml(options = {})     require 'builder'     options[:indent] ||= 2     xml = options[:builder] ||= ::builder::xmlmarkup.new(indent: options[:indent])     xml.instruct! :xml, :version=>"1.0", :encoding => "iso-8859-1"     xml.level_one       xml.tag!(:second_level, 'content')     end   end end 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -