How to remove <hash></hash> from format.xml
Asked Answered
C

3

6

I have an object that has_many child objects that should be rendered as xml. This is not a problem. My Problem is that I creat a Hash containing this data like the parser needs it. But rails atomaticly encloses the whole file with

<hash>
    <objects type="array">
        <object>
           ...
           ...
           ...
        </object>
    </objects>
</hash>

I need to get rid of the type="array" and the <hash> how can I handle this? I didnt find anything on the documentation.

Crouch answered 11/1, 2012 at 12:43 Comment(4)
Have you tried to_xml method? #1740405Ladder
Looks like this is exactly your case #6242411Ladder
Thank you that did a part I found the rest on my own => :skip_types => trueCrouch
@Crouch post it like an answerName
C
1
render :xml => @objects.to_xml(:root => :root_name, :skip_types => true)
Crouch answered 11/1, 2012 at 16:10 Comment(2)
Now I have the tag <root-name> in my output xml... Not the intent of the question as I read it. The "array" is gone, but <hash> is just renamed.. it's still just as there.Contraband
looks fine:? [1,2,3].to_xml(:root => :objects, :skip_types => true) => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<objects>\n <object>1</object>\n <object>2</object>\n <object>3</object>\n</objects>\n"Irreligion
G
8

I was having the same Issue;

This is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<Contacts>
  <Contact type="array">
  </Contact>
</Contacts>

I was using this:

entries.to_xml

to convert hash data into XML, but this wraps entries' data into <hash></hash>

So I modified:

entries.to_xml(root: "Contacts")

but that still wrapped the converted XML in 'Contacts' modifying my XML code to

<Contacts>
 <Contacts>
  <Contact type="array">
   <Contact>
    <Name></Name>
    <Email></Email>
    <Phone></Phone>
   </Contact>
  </Contact>
 </Contacts>
</Contacts>

So it adds an extra ROOT that I don't wan't there.

Now solution to this what worked for me is:

 entries["Contacts"].to_xml(root: "Contacts")

that avoids <hash></hash> or any additional root to be included. Cheers!!

Gerlach answered 30/9, 2013 at 8:41 Comment(0)
C
1
render :xml => @objects.to_xml(:root => :root_name, :skip_types => true)
Crouch answered 11/1, 2012 at 16:10 Comment(2)
Now I have the tag <root-name> in my output xml... Not the intent of the question as I read it. The "array" is gone, but <hash> is just renamed.. it's still just as there.Contraband
looks fine:? [1,2,3].to_xml(:root => :objects, :skip_types => true) => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<objects>\n <object>1</object>\n <object>2</object>\n <object>3</object>\n</objects>\n"Irreligion
P
0

There are a variety of reasons when doing actual development in the actual world why you might need fragments of XML even if you're perfectly well aware that the actual XML spec dictates a single root.

However, the ActiveSupport's Hash#to_xml does not support this.

Here is a method which does (add in config/initializers/rootless_xml.rb):

class Hash
  def to_rootless_xml(options = {})
    require 'active_support/builder' unless defined?(Builder)

    options = options.dup
    options[:indent]  ||= 2
    options[:root]    ||= 'hash'
    options[:builder] ||= Builder::XmlMarkup.new(indent: options[:indent])

    builder = options[:builder]
    builder.instruct! unless options.delete(:skip_instruct)


    each { |key, value| ActiveSupport::XmlMini.to_tag(key, value, options) }
    yield builder if block_given?
    builder.target!
  end
end
Paleontography answered 6/2, 2017 at 2:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.