Ruby XML to JSON Converter?

Existe-t-il une bibliothèque pour convertir XML en JSON dans Ruby?

Un truc simple:

Tout d’abord, vous devez gem install json , puis, lorsque vous utilisez Rails, vous pouvez:

 require 'json' require 'active_support/core_ext' Hash.from_xml('5').to_json #=> "{\"variable\":\"5\"}" 

Si vous n’utilisez pas Rails, vous pouvez gem install activesupport , en avoir besoin et tout devrait fonctionner correctement.

Exemple:

 require 'json' require 'net/http' require 'active_support/core_ext/hash' s = Net::HTTP.get_response(URI.parse('https://stackoverflow.com/feeds/tag/ruby/')).body puts Hash.from_xml(s).to_json 

J’utiliserais Crack , un simple parsingur XML et JSON.

 require "rubygems" require "crack" require "json" myXML = Crack::XML.parse(File.read("my.xml")) myJSON = myXML.to_json 

Si vous souhaitez conserver tous les atsortingbuts, je vous recommande cobravsmongoose http://cobravsmongoose.rubyforge.org/ qui utilise la convention du blaireau.

 charliedavid 

devient:

 {"alice":{"@sid":"4","bob":[{"$":"charlie","@sid":"1"},{"$":"david","@sid":"2"}]}} 

code:

 require 'rubygems' require 'cobravsmongoose' require 'json' xml = 'charliedavid' puts CobraVsMongoose.xml_to_hash(xml).to_json 

Vous pouvez trouver la gemme xml-to-json utile. Il conserve les atsortingbuts, les instructions de traitement et les instructions DTD.

Installer

 gem install 'xml-to-json' 

Usage

 require 'xml/to/json' xml = Nokogiri::XML 'ayy lmao' puts JSON.pretty_generate(xml.root) # Use `xml` instead of `xml.root` for information about the document, like DTD and stuff 

Produit:

 { "type": "element", "name": "root", "atsortingbutes": [ { "type": "atsortingbute", "name": "some-attr", "content": "hello", "line": 1 } ], "line": 1, "children": [ { "type": "text", "content": "ayy lmao", "line": 1 } ] } 

C’est un simple dérivé de xml-to-hash .

En supposant que vous utilisez libxml, vous pouvez essayer une variante (avertissement, cela fonctionne pour mon cas d’utilisation limité, il peut avoir besoin d’être modifié pour être entièrement générique)

 require 'xml/libxml' def jasonized jsonDoc = xml_to_hash(@doc.root) render :json => jsonDoc end def xml_to_hash(xml) hashed = Hash.new nodes = Array.new hashed[xml.name+"_atsortingbutes"] = xml.atsortingbutes.to_h if xml.atsortingbutes? xml.each_element { |n| h = xml_to_hash(n) if h.length > 0 then nodes << h else hashed[n.name] = n.content end } hashed[xml.name] = nodes if nodes.length > 0 return hashed end 

Si vous êtes à la recherche de vitesse, je vous recommanderais Ox puisque c’est l’option la plus rapide de celles déjà mentionnées.

J’ai exécuté des tests de performances en utilisant un fichier XML contenant 1,1 Mo d’ omg.org / spec et voici les résultats (en secondes):

 xml = File.read('path_to_file') Ox.parse(xml).to_json --> @real=44.400012533 Crack::XML.parse(xml).to_json --> @real=65.595127166 CobraVsMongoose.xml_to_hash(xml).to_json --> @real=112.003612029 Hash.from_xml(xml).to_json --> @real=442.474890548