# Is it possible to make to\_xml use underscore instead of dash

**URL:** <https://www.ruby-forum.com/t/is-it-possible-to-make-to-xml-use-underscore-instead-of-dash/100667>\
**Category:** Rails\
**Created:** [July 31, 2006, 10:01am UTC](https://www.ruby-forum.com/t/is-it-possible-to-make-to-xml-use-underscore-instead-of-dash/100667 "2006-07-31T10:01:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [July 31, 2006, 10:01am UTC](https://www.ruby-forum.com/t/is-it-possible-to-make-to-xml-use-underscore-instead-of-dash/100667/1 "2006-07-31T10:01:45Z")

</div>

Hi all,

I am getting ActiveRecord to produce XML with

```
  render :xml => @user.to_xml

```

However, on the client end it is a bit annoying for me to deal with  
attributes like first-name. I would much rather have first\_name.

Is there any way for me to turn off the behavior of converting \_ to -?  
This seems to be done by a call to dasherize inside the to\_xml of  
ActiveSupport::CoreExtensions::Array::Conversions, so I’m a bit out of  
my depth…

Thanks,  
Peter A.

---

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [July 31, 2006, 10:17am UTC](https://www.ruby-forum.com/t/is-it-possible-to-make-to-xml-use-underscore-instead-of-dash/100667/2 "2006-07-31T10:17:20Z")

</div>

On 7/31/06, Peter A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This seems to be done by a call to dasherize inside the to\_xml of  
> ActiveSupport::CoreExtensions::Array::Conversions, so I’m a bit out of  
> my depth…

I just ran into that same issue, and no there isn’t an option to not  
dasherize… I just subclassed the Conversions module for both hashes  
and arrays and created a to\_xml\_nodash. All you have to do is get rid  
of the calls to dasherize, but you need to do it for array and hash,  
as hash calls array. I was just hacking on this tonight, following is  
the code I put at the top of application.rb. Probably a better idea  
to stick it in the lib directory and require it, but haven’t done that  
yet since I was just hacking on it this evening.

module ActiveSupport #:nodoc:  
module CoreExtensions #:nodoc:  
module Hash #:nodoc:  
module Conversions  
def to\_xml\_nodash(options = {})  
options[:indent] ||= 0  
options.reverse\_merge!({ :builder =\>  
Builder::XmlMarkup.new(:indent =\> options[:indent]), :root =\> “hash”  
})  
options[:builder].instruct! unless  
options.delete(:skip\_instruct)

```
      options[:builder]. __send__ (options[:root].to_s) do
        each do |key, value|
          case value
            when ::Hash
              value.to_xml_nodash(options.merge({ :root => key,

```

:skip\_instruct =\> true }))  
when ::Array  
value.to\_xml\_nodash(options.merge({ :root =\> key,  
:children =\> key.to\_s.singularize, :skip\_instruct =\> true}))  
else  
type\_name = XML\_TYPE\_NAMES[value.class.to\_s]

```
              options[:builder].tag!(key.to_s,
                XML_FORMATTING[type_name] ?

```

XML\_FORMATTING[type\_name].call(value) : value,  
options[:skip\_types] || value.nil? ||  
type\_name.nil? ? { } : { :type =\> type\_name }  
)  
end  
end  
end  
end  
end  
end  
end  
end  
module ActiveSupport #:nodoc:  
module CoreExtensions #:nodoc:  
module Array #:nodoc:  
module Conversions  
def to\_xml\_nodash(options = {})  
raise “Not all elements respond to to\_xml” unless all? { |e|  
e.respond\_to? :to\_xml\_nodash }

```
      options[:root] ||= all? { |e| e.is_a?(first.class) &&

```

first.class.to\_s != “Hash” } ? first.class.to\_s.underscore.pluralize :  
“re  
cords”  
options[:children] ||= options[:root].singularize  
options[:indent] ||= 2  
options[:builder] ||= Builder::XmlMarkup.new(:indent =\>  
options[:indent])

```
      root = options.delete(:root)
      children = options.delete(:children)

      options[:builder].instruct! unless 

```

options.delete(:skip\_instruct)  
options[:builder].tag!(root.to\_s) { each { |e|  
e.to\_xml\_nodash(options.merge({ :skip\_instruct =\> true, :root =\>  
children })) } }  
end  
end  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [July 31, 2006, 10:36am UTC](https://www.ruby-forum.com/t/is-it-possible-to-make-to-xml-use-underscore-instead-of-dash/100667/3 "2006-07-31T10:36:37Z")

</div>

Thanks!

(By the way, my wife thinks your email address is great 🙂
