Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Rake task to parse third party XML file in rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 697
    Comment on it

    Parsing a xml file means separating a data block into pieces by following an algorithms, so that we can be easily manipulate.

     

    If there are a requirement in rails application to read XMl file from remote, we can use the Nokogiri to read file from remote site. In this blog we are created a rake task to read remote xml file from url "http://www.kongregate.com/games_for_your_site.xml".


    Here we passed this url as a param to the open() method of XML() which fetched xml file from remote and we read all <game> nodes & its child nodes(like id, title, thumbnail, category, description,
     instructions etc.) from file.

     

    Example:

    Path: my_app/lib/tasks/soap.rake

    task :soap do
      xml_file_data = Nokogiri::XML(open("http://www.kongregate.com/games_for_your_site.xml"))
      xml_file_data.xpath("//game").each do |data|
        %w[id title thumbnail category description instructions].each do |d|
        #concat node name & its value 
         p "#{d}: #{data.at(d).text}"
        # puts data.at(d)
        # puts data.at(d).text
      end
     end
    end

     

    Run rake task by command:

    rake soap

     

    Output looks like:

    "ID: 446"
    "TITLE: Sprout"
    "THUMBNAIL: https://cdn2.kongcdn.com/game_icons/0000/0637/customlogic.sprout_icon.jpg?i10c=img.resize(width:93)"
    "CATEGORY: Adventure & RPG"
    "DESCRIPTION: A storybook adventure staring a tiny seed pod. Journey from your tiny island home to the lush forests of your dreams;  explore the hand-drawn landscape and meet some interesting friends along the way.\n"
    "INSTRUCTIONS: "
    "ID: 4041"
    "TITLE: N3wton"
    "THUMBNAIL: https://cdn3.kongcdn.com/game_icons/0000/7181/N3wton_120.jpg?i10c=img.resize(width:93)"
    "CATEGORY: Puzzle"
    "DESCRIPTION: Maneuver your character through 25 puzzle filled levels.\nUpdate:  Hopefully I&#8217;ve fixed a few problems with people not getting their badges.  Unfortunately this caused any of your saved games to disappear.  Sorry!\nA hint for level 21:  Read the clue carefully, and pay close attention to the words themselves.  Eventually you&#8217;ll get it ;)\nIf you find you can&#8217;t save the game, check to see if you have cookies enabled.  They need to be enabled in order to save the game.\n"
    "INSTRUCTIONS: Use WASD or the Arrow keys to move your character, click to fire.\n"

     

     

 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: