# Best way to parse XML data

**URL:** <https://www.ruby-forum.com/t/best-way-to-parse-xml-data/71037>\
**Category:** Rails\
**Created:** [October 4, 2006, 9:29pm UTC](https://www.ruby-forum.com/t/best-way-to-parse-xml-data/71037 "2006-10-04T21:29:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@Stever](https://www.ruby-forum.com/u/Stever)\
**Post date:** [October 4, 2006, 9:29pm UTC](https://www.ruby-forum.com/t/best-way-to-parse-xml-data/71037/1 "2006-10-04T21:29:10Z")

</div>

Hey,

I am hooking up with the USPS rate calculator so I can calculate  
shipping costs online. Unfortunately, USPS uses neither XML-RPC or  
SOAP. I send the data via a GET request (a long URL) and I get data  
back via xml. Questions:

1. Is there a library or something within the rails platform that  
allows me to send a get request?
2. Is there a library that allows me to parse the resulting XML data?

---

<div class="post-metadata">

**Author:** ![Stever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@Stever](https://www.ruby-forum.com/u/Stever)\
**Post date:** [October 4, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/best-way-to-parse-xml-data/71037/2 "2006-10-04T21:39:12Z")

</div>

> I am hooking up with the USPS rate calculator so I can calculate  
> shipping costs online. Unfortunately, USPS uses neither XML-RPC or  
> SOAP. I send the data via a GET request (a long URL) and I get data  
> back via xml. Questions:
> 
> 1. Is there a library or something within the rails platform that  
> allows me to send a get request?
> 2. Is there a library that allows me to parse the resulting XML data?

Never used it, but rexml maybe…

[http://www.germane-software.com/software/rexml\_doc/](http://www.germane-software.com/software/rexml_doc/)  
[http://www.germane-software.com/software/rexml/docs/tutorial.html](http://www.germane-software.com/software/rexml/docs/tutorial.html)

---

<div class="post-metadata">

**Author:** ![Stever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@Stever](https://www.ruby-forum.com/u/Stever)\
**Post date:** [October 4, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/best-way-to-parse-xml-data/71037/3 "2006-10-04T22:13:18Z")

</div>

On 10/4/06, Stever [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey,
> 
> I am hooking up with the USPS rate calculator so I can calculate  
> shipping costs online. Unfortunately, USPS uses neither XML-RPC or  
> SOAP. I send the data via a GET request (a long URL) and I get data  
> back via xml. Questions:
> 
> 1. Is there a library or something within the rails platform that  
> allows me to send a get request?
> 2. Is there a library that allows me to parse the resulting XML data?

A POST works just fine with the USPS webtools api. It’s just not  
evident from the documentation. Just post the data like you would for  
a regular form post using application/x-www-form-urlencoded as the  
content type.

Chris

---

<div class="post-metadata">

**Author:** ![Stever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@Stever](https://www.ruby-forum.com/u/Stever)\
**Post date:** [October 4, 2006, 10:45pm UTC](https://www.ruby-forum.com/t/best-way-to-parse-xml-data/71037/4 "2006-10-04T22:45:20Z")

</div>

If this is the case, do I use the “web services” functionality in  
rails? I can’t seem to find any documentation either on the USPS  
website or the internet for that matter. usps doesn’t even say if  
they’re using XML-RPC or SOAP (I think it’s neither).

Can you at least point me in the right direction?

---

<div class="post-metadata">

**Author:** ![Stever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@Stever](https://www.ruby-forum.com/u/Stever)\
**Post date:** [October 4, 2006, 11:07pm UTC](https://www.ruby-forum.com/t/best-way-to-parse-xml-data/71037/5 "2006-10-04T23:07:22Z")

</div>

On 10/4/06, Stever [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If this is the case, do I use the “web services” functionality in  
> rails? I can’t seem to find any documentation either on the USPS  
> website or the internet for that matter. usps doesn’t even say if  
> they’re using XML-RPC or SOAP (I think it’s neither).

Use something like net/https to do the POST. The only strange thing  
is that while the content type is application/x-www-form-urlencoded,  
you don’t want to actually urlencode any of the data.

Here is a page that shows how it’s done in perl.

[http://www.icdevgroup.org/docs/tags/usps-query.html](http://www.icdevgroup.org/docs/tags/usps-query.html)

---

<div class="post-metadata">

**Author:** ![Stever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@Stever](https://www.ruby-forum.com/u/Stever)\
**Post date:** [October 5, 2006, 8:16am UTC](https://www.ruby-forum.com/t/best-way-to-parse-xml-data/71037/6 "2006-10-05T08:16:01Z")

</div>

Also, is there someplace else I can post this code? There are a few  
sites that seem to be sites to store “code snippets”. I forgot their  
names though. What are the more popular ones?

---

<div class="post-metadata">

**Author:** ![Stever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@Stever](https://www.ruby-forum.com/u/Stever)\
**Post date:** [October 5, 2006, 8:14am UTC](https://www.ruby-forum.com/t/best-way-to-parse-xml-data/71037/7 "2006-10-05T08:14:01Z")

</div>

Guys,

I figured out a way to write a script to get the info I needed from  
USPS web tools. I used rexml and net/http plugins. I thought I would  
contribute code to this group because there doesn’t seem to be much  
support on the usps site for ruby. I searched online and couldn’t find  
any scripts either. Anyway, here you go:  
(PS, please don’t make fun of this code, I am new to ruby. However,  
this code seems to work for me).  
#! /usr/bin/ruby

# \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

# The purpose of this script is to find out how much it costs

# to send a package through USPS through a pre-defined

# criteria

# \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

# \*\*\* Define variables here \*\*\*

userid = “577MODEL4495”  
service = “PRIORITY”  
zip\_origination = “10022”  
zip\_destination = “20008”  
pounds = “10”  
ounces = “5”  
container = “Flat Rate Box”  
box\_size = “REGULAR”

# Administrative

require ‘uri’  
require ‘net/http’  
require “rexml/document”  
include REXML

# Prepares the xml request for sending

xml\_request = \<\<END  
  
  
#{service}  
#{zip\_origination}  
#{zip\_destination}  
#{pounds}  
#{ounces}  
#{container}  
#{box\_size}  
  
  
END  
xml\_request = xml\_request.gsub(/[\t\r\n]/,‘’)

# Sends the xml request to USPS

host = ‘[testing.shippingapis.com](http://testing.shippingapis.com)’  
path = “/ShippingAPITest.dll?API=RateV2&XML=” +  
“#{URI.encode(xml\_request)}”  
xml\_response = Net::HTTP.get host, path

# Parses the returned xml response

parsed = Document.new xml\_response  
puts parsed.elements[‘//RateV2Response/Package/Postage/Rate’].text
