如何从Rails中的代码解析和发送整个复杂的XML

时间:2016-08-04 20:23:32

标签: ruby xml ruby-on-rails-4 soap savon

所以我有这个复杂的XML并希望它将它解析为数组并由Savon发送到服务器。问题是如何解析参数?

<soapenv:Header>
    <add:MessageID
        xmlns:add="http://www.w3.org">sdhuf78dd67-8932
    </add:MessageID>
    <add:Action
        xmlns:add="http://www.w3.org/2005">http://google/FMP
    </add:Action>
    <add:To
        xmlns:add="http://www.w3.org/2005/08/addressing">https://no1.testbla.com/1HAD9ANA1
    </add:To>
    <link:TransactionFlowLink
        xmlns:link="http://google/2010/06/"/>
        <oas:Security
            xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <oas:UsernameToken oas1:Id="UsernameToken-1"
                xmlns:oas1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
                <oas:Username>AHOJHOLA</oas:Username>
                <oas:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">Nonce</oas:Nonce>
                <oas:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">HashedPassword</oas:Password>
                <oas1:Created>CurrentGMTDateAndTime</oas1:Created>
            </oas:UsernameToken>
        </oas:Security>
        <AMA_SecurityHostedUser
            xmlns="http://xml.amfds.com/2010/06">
            <UserID AgentDutyCode="DA" RequestorType="BO" PseudoCityCode="HIATRA67" POS_Type="5"/>
        </AMA_SecurityHostedUser>
</soapenv:Header>

我知道如何解析例如add:不带参数的Action:

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

我知道该参数应该用@前缀写。

但我不知道如何一起写。这是对的吗?

"add:Action" => {
    "@xmlns:add" => "http://www.w3.org/2005",
    "http://google/FMP"
},etc.

1 个答案:

答案 0 :(得分:1)

要了解这些信息,您需要查看Gyoku gem:Savon用于将Ruby哈希值转换为XML的gem。具体而言,the documentation on using explicit XML attributes。看一下,我们可以使用以下哈希来获取您正在寻找的XML:

{
  "add:Action" => {
    "@xmlns:add" => "http://www.w3.org/2005",
    :content! => "http://google/FMP"
  }
}

我们可以直接用Gyoku在IRB中测试这个:

irb> require 'gyoku'
# => true
irb> Gyoku.xml({"add:Action" => { "@xmlns:add" => "http://www.w3.org/2005", :content! => "http://google/FMP" } })
# => "<add:Action xmlns:add=\"http://www.w3.org/2005\">http://google/FMP</add:Action>"