ruby - 将 http 响应正文从 xml 转换为散列时出现问题 - 缺少属性引用错误

ruby - problem converting http response body from xml to hash - missing attribute quote error

我在将 HTTP 响应正文从 XML 转换为 ruby 中的哈希时遇到问题。我正在与 Soalrwinds Orion API.

互动
def submitCorePluginConfigRequest()
  corePluginContext = [{"name":"item 1"},{"name": "item2}]
  uri = URI.parse("https://examplesolarwinds.com:17778/SolarWinds/InformationService/v3/Json/Invoke/Orion.Discovery/CreateCorePluginConfiguration")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Post.new(uri.request_uri, initheader = { "Content-Type" => "application/json" })
  request.body = corePluginContext.to_json
  request.basic_auth("username", "password")

  response = http.request(request)
  hash_response = Hash.from_xml(response.body)

  return hash_response
end

Hash.from_xml 错误 missing attribute quote (REXML::ParseException)

response.body(格式化后)如下:

"<?xml version=\"1.0\" encoding=\"utf-16\"?>
<PluginItems>
    <knownTypes>
        <ArrayOfstring
            xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"
            xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
            <string>SolarWinds.Orion.Core.Models.Discovery.CoreDiscoveryPluginConfiguration,SolarWinds.Orion.Core.Models.V1</string>
        </ArrayOfstring>
    </knownTypes>
    <pluginItem>
        <ArrayOfDiscoveryPluginConfigurationBase
            xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"
            xmlns=\"http://schemas.datacontract.org/2004/07/SolarWinds.Orion.Core.Models.Discovery\">
            <DiscoveryPluginConfigurationBase
                xmlns:d2p1=\"http://schemas.solarwinds.com/2008/Orion\" i:type=\"d2p1:CoreDiscoveryPluginConfiguration\">
                <d2p1:ActiveDirectoryList />
                <d2p1:AddressRange />
                <d2p1:AgentsAddresses
                    xmlns:d3p1=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\" />
                    <d2p1:AutoImportVolumeTypes
                        xmlns:d3p1=\"http://schemas.datacontract.org/2004/07/SolarWinds.Common.Snmp\" i:nil=\"true\" />
                        <d2p1:BulkList
                            xmlns:d3p1=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
                            <d3p1:string>10.83.4.77</d3p1:string>
                        </d2p1:BulkList>
                        <d2p1:Credentials>
                            <d2p1:credentials>
                                <knownTypes>
                                    <ArrayOfstring
                                        xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"
                                        xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\" />
                                    </knownTypes>
                                    <pluginItem>
                                        <d2p1:ArrayOfCredential
                                            xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" />
                                        </pluginItem>
                                    </d2p1:credentials>
                                </d2p1:Credentials>
                                <d2p1:DiscoverAgentNodes>false</d2p1:DiscoverAgentNodes>
                                <d2p1:PreferredPollingMethod>SNMP</d2p1:PreferredPollingMethod>
                                <d2p1:SharedCredentials
                                    xmlns:d3p1=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\" />
                                    <d2p1:SubnetList />
                                    <d2p1:WMICredentials />
                                    <d2p1:WmiRetries>0</d2p1:WmiRetries>
                                    <d2p1:WmiRetryInterval>PT1S</d2p1:WmiRetryInterval>
                                </DiscoveryPluginConfigurationBase>
                            </ArrayOfDiscoveryPluginConfigurationBase>
                        </pluginItem>
                    </PluginItems>"

当我复制此 xml 并将其显式声明为字符串时(即 testxml = above xml),Hash.from_xml(testxml) 工作正常并输出散列。 上面的 xml 是有效的, response.body 有引号所以我不明白为什么它说它缺少引号。 我试过使用response.parsed_response,我试过使用response.body.to_s,我试过使用XML simple.

好的,我找到了适合我的东西。

请求后,我尝试使用 JSON 解析器:

json_response = JSON.parse(response.body)

但这只是将响应主体变成了稍微不同的 xml,而不是 JSON。 但随后使用不同的 xml、hash_response = Hash.from_xml(json_response) 将响应转换为散列。

不完全确定这一切是怎么回事,但它有效。