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.
I greatly prefer using Nokogiri for my XML parser:
Or: