Tuesday 15 July 2014

How to convert XML to hash in Rails where empty array and nil values are preserved -


When I convert a XML structure into the hash from the train to Hash.from_xml (@xml) I do not distinguish the parser between the empty arrays and zero values, while the xml shows the nodes that are immediately eliminated from the form of an empty array as \ , e.g. & lt; Audio_languages ​​/ & gt; vs. properties with which the attribute zero = "true" is defined as zero values.

XML structure (which controls how I generate) looks like this:

  & lt; Feedback & gt; & Lt; Medias & gt; & Lt; Media & gt; & Lt; ID & gt; 1 & lt; / Id & gt; & Lt; Name & gt; Media-1 & lt; / Name & gt; & Lt; Audio_languages ​​/ & gt; & Lt; Avg_rating nil = "true" & gt; & Lt; / Avg_rating & gt; & Lt; / Media & gt; & Lt; Media & gt; & Lt; ID & gt; 2 & lt; / Id & gt; & Lt; Name & gt; Media-2 & lt; / Name & gt; & Lt; Audio_languages ​​/ & gt; & Lt; Avg_rating nil = "true" & gt; & Lt; / Avg_rating & gt; & Lt; / Media & gt; & Lt; / Medias & gt; & Lt; / Reaction & gt; Expected output from   

Hash.from_xml (@xml) will be:

  {"response" = & gt; {"Medias" = & gt; {"Media" = & gt; [{"Id" = & gt; "1", "name" = & gt; "Media-1", "audio_languages" = & gt; [], "Avg_rating" = & gt; Niil}, {"id" = & gt; "2", "name" = & gt; "Media-2", "Audio_Languages" = & gt; [], "Avg_rating" = & gt; Instead, I get zero values ​​for  audio_languages ​​ and  avg_rating : ]}}}   

P>

  {"response" = & gt; {"Media" = & gt; {"Media" = & gt; [{"Id" = & gt; "1", "name" = & gt; "Media-1", "audio_languages" = & gt; Zero, "AVG_RATING" = & gt; Zero}, {"id" = & gt; "2", "name" = & gt; "media-2", "audio_languages" = & gt; Zero, "AVG_RATING" = & gt; Zero}]}}}    

I used the libxml and I I have finished parsing the nodes using I if I checked the nodes sign I'm checking to figure out if I want to convert a zero value to an empty array such as

  #Usage:. Hash.from_xml_with_libxml (xml) is required 'xml / lib xml' # # http://movesonrails.com/articles/2008/02/25/libxml-for-active-resource-2-0 Adapted from class hash class & Lt; & Lt; Self def by_xml_with_libxml (xml, strict = true) LibXML :: XML.default_load_external_dtd = false LibXML :: XML.default_pedantic_parser = strict results = LibXML :: XML :: Parser.string (xml) .parse return {result.root.name. To_s = & gt; Xml_node_to_hash_with_libxml (result.root)} End Def xml_node_to_hash_with_libxml (node) # If we are the foundation of the document, hash start the node.element? If node children? Result_hash = {} node. Do every_child | Children Result = xml_node_to_hash_with_libxml (child) if child.name == "text" if! Child.next? And! Child.prev? If results_ship [child.name] .is_a (object :: array) results_ship [child.name] , are arrays, and # like & lt nodes; AVERAGE_RATING "zero" = "true" /> Nil values ​​are if node.to_s.match (/ ^ \ & lt; (. +) \ / \ & Gt = $ /) & amp; Amp; Zero == node. Warranty ["zero"] Return [] End return zero end and back node content. CT_O_S End End End End    

No comments:

Post a Comment