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:
<list-index>
<group>
<title>Taal op Maat Groep</title>
<list href=''http://www.wrts.nl/api/lists/23975143''>
<id>23975143</id>
<title>Taal op Maat Groep 3 Week 1</title>
<lang-a>Nederlands</lang-a>
<lang-b>Nederlands</lang-b>
<created-on>2009-12-03T22:40:33+01:00</created-on>
<updated-on>2009-12-03T22:40:33+01:00</updated-on>
<word-count>10</word-count>
<shared href=''http://www.wrts.nl/api/shared/
woordenlijstendigischool''>true</shared>
<download-count>0</download-count>
<result-count href=''http://www.wrts.nl/api/lists/23975143/
results''>0</result-count>
</list>
<list href=''http://www.wrts.nl/api/lists/23975144''>
<id>23975144</id>
<title>Taal op Maat Groep 3 Week 2</title>
<lang-a>Nederlands</lang-a>
<lang-b>Nederlands</lang-b>
<created-on>2009-12-03T22:40:42+01:00</created-on>
<updated-on>2009-12-03T22:40:42+01:00</updated-on>
<word-count>9</word-count>
<shared href=''http://www.wrts.nl/api/shared/
woordenlijstendigischool''>true</shared>
<download-count>0</download-count>
<result-count href=''http://www.wrts.nl/api/lists/23975144/
results''>0</result-count>
</list>
...
<list href=''http://www.wrts.nl/api/lists/24743885''>
<id>24743885</id>
<title>Taal Actief Groep 8 woordpakket 1</title>
<lang-a>Nederlands</lang-a>
<lang-b>Nederlands</lang-b>
<created-on>2010-01-02T13:28:19+01:00</created-on>
<updated-on>2010-01-02T13:28:19+01:00</updated-on>
<word-count>24</word-count>
<shared href=''http://www.wrts.nl/api/shared/
woordenlijstendigischool''>true</shared>
<download-count>0</download-count>
<result-count href=''http://www.wrts.nl/api/lists/24743885/
results''>0</result-count>
</list>
</group>
</list-index>
*****
What am i doing wrong here?
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
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?
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On 20 jan, 17:43, Peter Hickman <peterhickman...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> 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}</a> /n"} It just prints the whole array. How can i fix this? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.