在 Ruby 中使用 libxml 解析 SOAP 响应

发布于 2024-08-30 06:56:37 字数 637 浏览 4 评论 0原文

我正在尝试解析来自 Savon SOAP api 的以下 SOAP 响应

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:getConnectionResponse xmlns:ns="http://webservice.jchem.chemaxon">
            <ns:return>
                <ConnectionHandlerId>connectionHandlerID-283854719</ConnectionHandlerId>
            </ns:return>
        </ns:getConnectionResponse>
    </soapenv:Body>
</soapenv:Envelope>

我正在尝试使用 libxml-ruby 但没有成功。基本上我想提取标签内的任何内容和 connectionHandlerID 值。

I am trying to parse following SOAP response coming from Savon SOAP api

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:getConnectionResponse xmlns:ns="http://webservice.jchem.chemaxon">
            <ns:return>
                <ConnectionHandlerId>connectionHandlerID-283854719</ConnectionHandlerId>
            </ns:return>
        </ns:getConnectionResponse>
    </soapenv:Body>
</soapenv:Envelope>

I am trying to use libxml-ruby without any success. Basically I want to extract anything inside tag and the connectionHandlerID value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乙白 2024-09-06 06:56:37

当您使用 Savon 时,您可以将响应转换为哈希值。转换方法 response.to_hash 还可以为您做一些其他有用的事情。

然后,您将能够使用类似于以下的代码获取所需的值

hres = soap_response.to_hash
conn_handler_id = hres[:get_connection_response][:return][:connection_handler_id]

查看文档

As you are using Savon you can convert the response to a hash. The conversion method response.to_hash does some other useful things for you as well.

You would then be able to get the value you want using code similar to the following

hres = soap_response.to_hash
conn_handler_id = hres[:get_connection_response][:return][:connection_handler_id]

Check out the documentation

简单爱 2024-09-06 06:56:37

我推荐 nokogiri

假设您的 XML 响应位于名为 response 的对象中。

require 'nokogiri'
doc = Nokogiri::XML::parse response
doc.at_xpath("//ConnectionHandlerId").text

I'd recommend nokogiri.

Assuming your XML response is in an object named response.

require 'nokogiri'
doc = Nokogiri::XML::parse response
doc.at_xpath("//ConnectionHandlerId").text
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文