I am using libxml-ruby gem to read a xml string.
I am unable to extract the required info from xml string.
Currently i have following xml
<?xml version="1.0" encoding="utf-8"?>
<message>
<head>
<api_key>252f5df2df522fg5fd25df2df5df2fd5</api_key>
<user>123</user>
<secret>********************</secret>
<signature>****************</signature>
<synchronization token="kj0s09ew090mv904v09409905b" last_synchronize_on="2010-01-02 11:30" />
</head>
<blockings>
<blocking token="AAA" start_time="2010-01-01 10:00" end_time="2010-01-01 12:00" method="REQUEST" is_forced="FALSE" />
<blocking token="BBB" start_time="2010-01-03 15:00" end_time="2010-01-03 18:00" method="REQUEST" is_forced="FALSE" />
</blockings>
</message>
How do i get api_key, synchronization token, last_synchronize_on values from this xml string?
How do i get the info of the blocking nodes inside blockings node of this string?
Thanks
You’d use LibXML::XML::Parser to get a LibXML::XML::Document:
Then use
findand a bit of XPath to find your nodes:That gives you a
LibXML::XML::Nodeinapi_key_nodeso you can callcontentto get what you want:Similar things apply to synchronization but you’d use
[]to access the attribute values:Same deal again for the
<blocking>nodes but you’d iterate through them witheachinstead of usingfirst.