Friday, 15 June 2012

XML to JSON using Ruby and save it for separate file -


Ruby programmers have got a new job to convert novice and huge XML and to save it to isolate JSON file.

< P> For example:
  & lt; Listing & gt; & Lt; ID & gt; Abc12345 & lt; / Id & gt; & Lt; Name & gt; BCD & lt; / Name & gt; & Lt; Address & gt; 12 Main Street & lt; / Address & gt; & Lt; / List & gt; & Lt; Listing & gt; & Lt; ID & gt; A1b2c3d4 & lt; / Id & gt; & Lt; Name & gt; XYZ & lt; / Name & gt; & Lt; Address & gt; 14 Main Street & lt; / Address & gt; & Lt; / List & gt; & Lt; Listing & gt; & Lt; ID & gt; Bcde45678 & lt; / Id & gt; & Lt; Name & gt; MNO & lt; / Name & gt; & Lt; Address & gt; 14 Broadway & lt; / Address & gt; & Lt; / List & gt;   

I want to save it to a different file and would like to use the ID because the name of the file abc12345.json, a1b2c3d4.json and bcde45678.json on this example is something like Will I have any problem on this issue: {"id": "bcde45678", "name": "MNO", "address": "14 broadway"}}

I can really help you for all future support.

I assume that you are listing Block in individual files Want to print as JSON if you have access to 'active_support / core_ext' and 'nosy', and you are not worried about how your XML has changed in JSON, you can:

  Required 'active_support / core_ext' required 'nocagory' xml = nautygree :: XML (file. Increase "yourfile") xml.search ("// list"). Every do L File name = l.at_xpath ("id"). Content file Open (filename + '.json', 'w') do. File | File.print Hash.from_xml (l.to_xml) .to_json End End    

No comments:

Post a Comment