# Pulling XML from Amazon API

**URL:** <https://www.ruby-forum.com/t/pulling-xml-from-amazon-api/89701>\
**Category:** Ruby\
**Created:** [January 5, 2006, 6:38am UTC](https://www.ruby-forum.com/t/pulling-xml-from-amazon-api/89701 "2006-01-05T06:38:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jotto](https://www.ruby-forum.com/u/jotto)\
**Post date:** [January 5, 2006, 6:38am UTC](https://www.ruby-forum.com/t/pulling-xml-from-amazon-api/89701/1 "2006-01-05T06:38:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jotto](https://www.ruby-forum.com/u/jotto)\
**Post date:** [January 5, 2006, 6:47am UTC](https://www.ruby-forum.com/t/pulling-xml-from-amazon-api/89701/2 "2006-01-05T06:47:08Z")

</div>

On 1/5/06, jotto [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![jotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jotto](https://www.ruby-forum.com/u/jotto)\
**Post date:** [January 5, 2006, 6:59am UTC](https://www.ruby-forum.com/t/pulling-xml-from-amazon-api/89701/3 "2006-01-05T06:59:10Z")

</div>

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/](http://ruby-doc.org/stdlib/)

Devin

---

<div class="post-metadata">

**Author:** ![jotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jotto](https://www.ruby-forum.com/u/jotto)\
**Post date:** [January 5, 2006, 6:53am UTC](https://www.ruby-forum.com/t/pulling-xml-from-amazon-api/89701/4 "2006-01-05T06:53:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jotto](https://www.ruby-forum.com/u/jotto)\
**Post date:** [January 5, 2006, 7:29am UTC](https://www.ruby-forum.com/t/pulling-xml-from-amazon-api/89701/5 "2006-01-05T07:29:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jotto](https://www.ruby-forum.com/u/jotto)\
**Post date:** [January 5, 2006, 11:53am UTC](https://www.ruby-forum.com/t/pulling-xml-from-amazon-api/89701/6 "2006-01-05T11:53:53Z")

</div>

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  
> ([Ruby/Amazon](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](http://www.braveworld.net/riva)
