Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • JSON and XML

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 210
    Comment on it

    JSON VS XML (RUBY)

    JSON stands for JavaScript Object Notation. XML stands for EXtensible Markup Language. Both Json and Xml are used for exchanging data.

    JSON is a light-weight text-based open standard design for human-readable data. It is mostly used format for exchanging data between cross platform languages. It originate from the JavaScript language.

    JSON is easy and simple for machines to generate or parse. That makes it ideal to interchange format.

    XML tags identify the data and are used to store and organize the data. The XML specifications do not match the data model for most programming languages. It is slower compared to JSON also its tedious for programmers to parse.

    A employees record in json can be defined as :

    {"employees":[
        {"firstName":"Johnny", "lastName":"Dork"},
        {"firstName":"Anne", "lastName":"Swan"},
        {"firstName":"Peterson", "lastName":"Jacob"}
    ]}
    

    The same records can be defined in XML as below:

    <employees>
        <employee>
            <firstName>Johnny</firstName> <lastName>Dork</lastName>
        </employee>
        <employee>
            <firstName>Anne</firstName> <lastName>Swan</lastName>
        </employee>
        <employee>
            <firstName>Peterson</firstName> <lastName>Jacob</lastName>
        </employee>
    </employees>
    

    How to parse json:

    To parse a JSON string received by another application or generated within your existing application:

    require 'json'

    my_hash = JSON.parse('{"hello": "programmer"}') puts my_hash["hello"] => "programmer"

    Notice the extra quotes '' around the hash notation. Ruby expects the argument to be a string and cant convert objects like a hash or array. Ruby converts string into a hash

    How to create json: Creating a JSON string for communication or serialization is just as simple.

    require 'json'

    my_hash = {:hello => "goodbye"} puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"

    Or an alternative way:

    require 'json' puts {:hello => "goodbye"}.to_json => "{\"hello\":\"goodbye\"}"

    Parsing XML with nokogiri:

    Nokogiri is used to get data from web or file and can parse xml & html.

    With nokogiri you can parse strings and can fetch nodes. With Nokogiri you can edit documents, which means you can add or delete nodes.

    require 'nokogiri'
    xml_string = << eos 
    <employees>
        <employee>
            <firstName>Johnny</firstName> <lastName>Dork</lastName>
        </employee>
        <employee>
            <firstName>Anne</firstName> <lastName>Swan</lastName>
        </employee>
        <employee>
            <firstName>Peterson</firstName> <lastName>Jacob</lastName>
        </employee>
    </employees>
             eos
    

    parsed_xml = Nokogiri::XML xml_string

    fetching data from parsed xml

    1. fetch first employee node

      parsed_xml.at_xpath("//employee")

    2. fetch first employee name

      parsed_xml.at_xpath("//employee/firstName").content OR parsed_xml.at_xpath("//employee/firstName").text OR parsed_xml.at_xpath("//employee").at_xpath('//firstName').content

    How to get all employee nodes:

    parsed_xml.xpath("//employee") => #returning an array of Keyword elements across the entire document, including at the root, and item levels.

    For more details please visit the url "http://www.nokogiri.org/tutorials/parsing_an_html_xml_document.html"

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: