views:

78

answers:

2

Hi All,

Sorry for this question but after spending 1-2 hours on how to read xml, i thought posting it on forum will be better.
So i get a complex (very large)xml response from the plugin trackify. i want to read some values from it so i convert it into hash and then read it as follows For ex:- to read city

@tracking_info['TrackResponse']['Shipment']['ShipTo']['Address']['City'] #>>  "SEATTLE"


my question is it proper way to getting xml response or there are some xml methods which is simple to use?

+2  A: 

You might want to take a look at the Nokogiri gem which makes this sort of thing easy.

John Topley
+1  A: 

If converting to hash works for you, use it. However, working with XML is not much harder, given the libraries available. REXML should be included in the Core, and it gives you basic XPath support to access the nodes by path:

s = "<a><b>1</b><b><c>2</c></b><c>pola</c><b>3</b></a>"
x = REXML::Document.new s
x.root.elements['/a/b/c'].text
#=> "2"

Take a look at the tutorial.

Mladen Jablanović