Parse xml data from xml response.body

Hello i’m trying to get a list of titles including the link to the
corresponding list out of a xml response from an API.

This is the code i wrote:

require ‘net/http’
require ‘rexml/document’

login = “***”
password = “***”

Net::HTTP.start(‘www.wrts.nl’) {|http|
req = Net::HTTP::Get.new(’/api/lists’)
req.basic_auth login, password
response = http.request(req)
xml_data = (response.body)

doc = REXML::Document.new(xml_data)
names = REXML::XPath.match( doc, “/list-index//list” )
puts names.inner_text()
}


The response.body looks like this:

Taal op Maat Groep 23975143 Taal op Maat Groep 3 Week 1 Nederlands Nederlands 2009-12-03T22:40:33+01:00 2009-12-03T22:40:33+01:00 10 true 0 0 23975144 Taal op Maat Groep 3 Week 2 Nederlands Nederlands 2009-12-03T22:40:42+01:00 2009-12-03T22:40:42+01:00 9 true 0 0 ... 24743885 Taal Actief Groep 8 woordpakket 1 Nederlands Nederlands 2010-01-02T13:28:19+01:00 2010-01-02T13:28:19+01:00 24 true 0 0 *****

What am i doing wrong here?

Well REXML::XPath.match( doc, “/list-index//list” ) will return a list
of
list elements so I wouldn’t expect inner_text() to make a lot of sense.
Perhaps something like this

titles = REXML::XPath.match( doc, “/list-index/group/list/title/text()”
)
titles.each{|title| puts title}

would work?

On 20 jan, 17:43, Peter H. [email protected]
wrote:

Well REXML::XPath.match( doc, “/list-index//list” ) will return a list of
list elements so I wouldn’t expect inner_text() to make a lot of sense.
Perhaps something like this

titles = REXML::XPath.match( doc, “/list-index/group/list/title/text()” )
titles.each{|title| puts title}

would work?

Yes it worked for me. It returns data in an array.
But i’m not able to print some html with the variable in it.

titles.each{|title| puts “<a href=”#">#{title} /n"}

It just prints the whole array.

How can i fix this?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs