Uchenna
Uchenna

Reputation: 4089

How to Process XML response in rails

I am building a simple domain name app that connects to dynadot xml api. i am using httparty to make the request to the api with the code below

  def index
    reqd = HTTParty.get("https://api.dynadot.com/api3.xml?key=xxxxx&command=search&domain0=mydomain.com&domain1=mydomain.net").body
    puts reqd
  end

and i get the following output

<Results>
   <SearchResponse><SearchHeader>
   <SuccessCode>0</SuccessCode>
   <DomainName>mydomain.com</DomainName>
   <Status>success</Status>
   <Available>no</Available>
   </SearchHeader></SearchResponse>
   <SearchResponse><SearchHeader>
   <SuccessCode>0</SuccessCode>
   <DomainName>mydomain.net</DomainName>
   <Status>success</Status>
   <Available>no</Available>
   </SearchHeader></SearchResponse></Results>

is it possible to pass this in rails and represent it as an array were i can loop through all the domain names and display them as a list with the available or not and the domain name beside it when converted to html. if possible i would like to know how with a code sample or explanation. Thank you.

Upvotes: 0

Views: 927

Answers (1)

HannesFostie
HannesFostie

Reputation: 485

Here's a simple way to do this:

# require 'active_support/core_ext/hash/conversions'
hash = Hash.from_xml(your_xml)

The your_xml variable is a string like the one you pasted. You could also parse the XML using Nokogiri and then use #to_s and pass that as an argument to Hash#from_xml

Upvotes: 2

Related Questions