Varun Krishnamurthy
Varun Krishnamurthy

Reputation: 1

What are the advantages and disadvantages of using from_xml function in ruby

I want to use the from_xml method provided by the rails active support core ext,

An example :

hash = Hash.from_xml("my_xml.xml")

for converting XML into a Hash.

I want to use this because parsing a Hash is a lot easier than XML in ruby.

However, I would want to know what are the pros and cons of using this approach. Is there a better approach I can use for converting an XML into hash.

Thanks

Upvotes: 0

Views: 100

Answers (1)

max
max

Reputation: 101811

XML unlike JSON is a document format and not just data exchange format and not always actually maps cleanly to programming language constructs like hashes. XML is actually ridiculously complex if you look at all the features like namespaces.

Hash.from_xml really only handles the simplest of cases and doesn't have a clue how to deal with stuff like attributes. It really only knows how to parse the XML generated by Hash#to_xml.

Advantages:

  • its so naive that its cute

Disadvantages:

  • see advantages

For non-trivial examples you'll need an actual XML parser like Nokogiri.

Upvotes: 1

Related Questions