Pulling XML from Amazon API


#1

Take Amazon’s API for example, returning a XML file. In my application
(I’m using Rails), I want to allow the user to specify a criteria,
input it into a URL and send that URL to amazon to get the resulting
XML file. My question is… how in Ruby can I grab the XML and put it
into a variable so I can parse it?


#2

On 1/5/06, jotto removed_email_address@domain.invalid wrote:

Take Amazon’s API for example, returning a XML file. In my application
(I’m using Rails), I want to allow the user to specify a criteria,
input it into a URL and send that URL to amazon to get the resulting
XML file. My question is… how in Ruby can I grab the XML and put it
into a variable so I can parse it?

Heres a starter for .2 Note the use of a Proxy.

Net::HTTP::Proxy(phost, pport, puser, ppwd).start(host) {|http|
xmlStr = http.get(loc).body.to_s

}
}

REXML will let you parse the XML String into a DOM object for further
munging.


#3

jotto wrote:

is that a common way of getting data from an API and manipulating it?

Isn’t amazon’s API xmlrpc or soap?

Ruby’s std library includes thingies for both:
http://ruby-doc.org/stdlib/

Devin


#4

is that a common way of getting data from an API and manipulating it?


#5

OK, I see now how to use Amazon’s through SOAP or XML RPC… but let’s
say the only way to access a particular API is by accessing a URL and
getting XML in return? Is this where you would use a proxy?


#6

jotto wrote:

OK, I see now how to use Amazon’s through SOAP or XML RPC… but let’s
say the only way to access a particular API is by accessing a URL and
getting XML in return? Is this where you would use a proxy?
The use of the proxy is optional (if there is a proxy inbetween then you
need to specify it)
Net::HTTP::Proxy returns a Net::HTTP object if phost is nil, e.g. no
proxy in between.
An alternative is the use of openuri so that you can do IO as if on an
XML file
You can also read the Ruby/Amazon library code
(http://www.caliban.org/ruby/ruby-amazon.shtml) for a solution on how to
handle XML content of HTTP requests.
Cheers,
V.-


http://www.braveworld.net/riva