Lasse
(Lasse)
January 20, 2010, 3:00pm
1
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()
}
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?
Lasse
(Lasse)
January 20, 2010, 6:27pm
3
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}</a> /n"}
It just prints the whole array.
How can i fix this?