ruby-将HTTP响应正文从xml转换为哈希的问题-缺少属性引号错误

时间:2018-11-02 07:02:56

标签: ruby xml hashtable

我无法将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简单。

1 个答案:

答案 0 :(得分:0)

好吧,我发现了一些对我有用的东西。

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

json_response = JSON.parse(response.body)

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

不能完全确定这是怎么回事,但是可以。