Closed fazelmk closed 9 years ago
Could you, please, tell me what version of Ruby and xml-simple you're using?
Using xml-simple 1.1.5 ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux] Rails 4.1.1
I do not have the test file you're using, so I did the following on my Mac:
2.1.2 :001 > require 'xmlsimple'
=> true
2.1.2 :002 > hash = {"test"=>{"name"=>"LE", "value"=>{"class"=>"string", "content"=>"0.0"}}}
=> {"test"=>{"name"=>"LE", "value"=>{"class"=>"string", "content"=>"0.0"}}}
2.1.2 :003 > XmlSimple.xml_out(hash, KeepRoot: true)
=> "<test name=\"LE\">\n <value class=\"string\">0.0</value>\n</test>\n"
i'm sorry. didn't see that the exemple I pasted was broked
require 'xmlsimple'
hash = XmlSimple.xml_in('<test><name>test</name><value class="int">1</value></test>', {KeepRoot: true, ForceArray: false})
=> {"test"=>{"name"=>"test", "value"=>{"class"=>"int", "content"=>"1"}}}
XmlSimple.xml_out(hash, KeepRoot: true)
=> "<test name=\"test\">\n <value class=\"int\">1</value>\n</test>\n"
the xml_in transforms the first element name in a attribute of test when it creates the hash, making the final output wrong.
Wasn't broken, just forgot to past as code =D
What do you expect instead?
The same xml as the input, since I din't make any changes to the hash.
The xml_in should treat child element as elements. But instead it put the child element an attribute of the parent element.
It's worth mentioning that elements with child elements or attributes are parsed as expected.
expected output of xml_out for the exemple would be
"<test>\n<name>test</name>\n<value class=\"int\">1</value>\n</test>\n"
Now I see. From xml-simple's documentation:
"The API offers little control over the output of xml_out. In particular, it is not especially likely that feeding the output from xml_in into xml_out will reproduce the original XML (although passing the output from xml_out into xml_in should reproduce the original data structure)."
Hope this helps!