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

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

所以我有这个复杂的 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.

要了解这些信息,您需要去看看Gyoku gem: the gem that Savon uses to translate Ruby hashes into XML. Specifically, the documentation on using explicit XML attributes。看一下,我们可以通过以下哈希得到您要查找的 XML:

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

我们可以直接在 IRB 中使用 Gyoku 进行测试:

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>"