Rails XML parsing -
Can anyone tell me how to parse it on the rail.
& lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Message & gt; & Lt; Param & gt; & Lt; Name & gt; MessageType & lt; / Name & gt; & Lt; Price & gt; SMS & lt; / Pricing & gt; & Lt; / Param & gt; & Lt; Param & gt; & Lt; Name & gt; Id & lt; / Name & gt; & Lt; Price & gt; Xxxxxxxxxxxxxx & lt; / Pricing & gt; & Lt; / Param & gt; & Lt; Param & gt; & Lt; Name & gt; Source & lt; / Name & gt; & Lt; Price & gt; Xxxxxxxxxxx & lt; / Pricing & gt; & Lt; / Param & gt; & Lt; Param & gt; & Lt; Name & gt; Goal & lt; / Name & gt; & Lt; Price & gt; Xxxxxxxxxxxxx & lt; / Pricing & gt; & Lt; / Param & gt; & Lt; Param & gt; & Lt; Name & gt; Msg & lt; / Name & gt; & Lt; Price & gt; Xxxxxxxxxxxxx & lt; / Pricing & gt; & Lt; / Param & gt; & Lt; Param & gt; & Lt; Name & gt; UDH & lt; / Name & gt; & Lt; Value & gt; & Lt; / Pricing & gt; & Lt; / Param & gt; & Lt; / Messages & gt;
I have no control over this XML, but I hope that I can look like this before saving the parameter to my database
Message "= & gt; {" Msg "=>" Sampler "," id "=>" 1 "," target "=>" 23123 "," source "=> 312321312} / Code> < [{"name" => "id", "value">
message "=> = & gt; "2373084120100804002252"}, {"name" = & gt; "MessageType", "value" = & gt; "SMS"}, {"name" = & gt; "Target", "value" = & gt; "23730841"}, "value" = & gt; "Source", "value" = & gt; "09156490046"}, {"name" = & gt; "Msg", "value" = & gt; "Hello World via iPhone"}, {"name" => "Udh", "value" = & gt; Zero}]
There are lots of ruby XML parsing libraries However, if your XML is short , You can use the ActiveSupport hash extension 'from_xml':
hash.frame_xml (x) ["message"] ["param"]. Inject ({}). Results, AMM | | Results [AMM ["name"]] = AMM ["value"] results ending
=> {"msg" => "xxxxxxxxxxxxx", "message type" => "SMS" Udh "=> zero," id "=>" xxxxxxxxxxxxxx "," target "=>" xxxxxxxxxxxxx "," source "=>" xxxxxxxxxxx "}
Comments
Post a Comment