使用Nokigiri(RUBY)解析XML文件

问题描述 投票:0回答:1

我正试图从我的xml文件中提取一些信息。

XML:

            <CONTENT>
            <dms:ComplexResponse ErrorCode="430" ErrorDescription="null :  PrivacyUE Mancante" Return="false" xmlns:dms="http://dmsmanagerservice">
                <dms:Element Name="DMSVERSION">2.7</dms:Element>
            </dms:ComplexResponse>
        </CONTENT>

我需要看到“ErrorCode”和“ErrorDescription”。

这是我的代码:

    require 'nokogiri'

    def see_error(response_xml.xml)


         doc = Nokogiri::XML(File.open(response_xml.xml))
         #it shows me the file

         doc.xpath('//dms:ComplexResponse/ErrorCode/ErrorDescription')


    end

之后它没有给我任何回报,只是一个空数组[]

你能帮我么?

ruby
1个回答
0
投票

命名空间需要注册:

doc.xpath('//dms:ComplexResponse', 'dms' => 'http://dmsmanagerservice').first.attributes['ErrorCode'].value
© www.soinside.com 2019 - 2024. All rights reserved.