Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove <hash></hash> from format.xml

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.

like image 643
davidb Avatar asked Jan 11 '12 12:01

davidb


1 Answers

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!!

like image 170
mayankcpdixit Avatar answered Sep 18 '22 00:09

mayankcpdixit