Search code examples
rubyxml-parsingrexml

ruby rexml: parsing xml files cleanly


I have the following XML file:

<Response Version="X">
  <Status StatusCode = "OK">
  <Internal>
    <SP>
      <URL>SP_url_1</URL>
    </SP>
    <SP>
      <URL>SP_url_2</URL>
    </SP>
  </Internal>
<Response>

I want to get all the URL elements' text in an array.

I implemented the following:

...perform an HTTP GET request...

response_xml = REXML::Document.new(response.body).root
if response_xml.nil? or response_xml.name != 'Response'
  raise ParseError, "Invalid service discovery response!"
end    
status_code  = response_xml.elements['Status'].attribute('StatusCode').value
if status_code == 'OK'
  urls = response_xml.elements.each('//URL') { |u| u }
end
urls

Regarding this line response_xml.elements.each('//URL') { |u| u }, can you recommend a cleaner way to get all URL elements in an array? Maybe there is a wiser way to do via REXML methods, but cannot see it right now.

Any other REXML or general code recommendations are welcome.


Solution

  • I greatly prefer using Nokogiri for my XML parser:

    require 'nokogiri'
    doc = Nokogiri::XML(<<EOT)
    <Response Version="X">
      <Status StatusCode = "OK">
      <Internal>
        <SP>
          <URL>SP_url_1</URL>
        </SP>
        <SP>
          <URL>SP_url_2</URL>
        </SP>
      </Internal>
    <Response>
    EOT
    
    if (doc.at('Status')['StatusCode'] == 'OK')
      urls = doc.search('URL').map(&:text)
      # => ["SP_url_1", "SP_url_2"]
    end
    

    Or:

    urls = doc.search('URL').map(&:text) if (doc.at('Status')['StatusCode'] == 'OK')