Boomerange Boomerange - 1 year ago 109
Ruby Question

How to parse and send whole complicated XML from code in Rails

So I have this complicated XML and want it to parse it to array and send by Savon to the server. The question is how can I parse parameters?

<oas:UsernameToken oas1:Id="UsernameToken-1"
<oas:Nonce EncodingType="">Nonce</oas:Nonce>
<oas:Password Type="">HashedPassword</oas:Password>
<UserID AgentDutyCode="DA" RequestorType="BO" PseudoCityCode="HIATRA67" POS_Type="5"/>

I know how to parse for example add:Action without parameter:

"add:Action" => "http://google/FMP"

And I know that parameter should be written with @ prefix.

But I dont know how to write it together. Is this right?

"add:Action" => {
"@xmlns:add" => "",

Answer Source

To find out this information, you need to go and have a look at the Gyoku gem: the gem that Savon uses to translate Ruby hashes into XML. Specifically, the documentation on using explicit XML attributes. Looking at that, we can get the XML you're looking for with the following hash:

  "add:Action" => {
    "@xmlns:add" => "",
    :content! => "http://google/FMP"

We can test this in IRB directly with Gyoku:

irb> require 'gyoku'
# => true
irb> Gyoku.xml({"add:Action" => { "@xmlns:add" => "", :content! => "http://google/FMP" } })
# => "<add:Action xmlns:add=\"\">http://google/FMP</add:Action>"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download