tags:

views:

42

answers:

4

I am working with an API and want to know how I can easily search and display/format the output based on the tags.

For example, here is the page with the API and examples of the XML OUtput:

http://developer.linkedin.com/docs/DOC-1191

I want to be able to treat each record as an object, such as User.first-name User.last-name so that I can display and store information, and do searches.

Is there perhaps a gem that makes this easier to do?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<people-search>
  <people total="108" count="10" start="0">
    <person>
      <id>tePXJ3SX1o</id>
      <first-name>Bill</first-name>
      <last-name>Doe</last-name>
      <headline>Marketing Professional and Matchmaker</headline>
      <picture-url>http://media.linkedin.com:/....&lt;/picture-url&gt;
    </person>
    <person>
      <id>pcfBxmL_Vv</id>
      <first-name>Ed</first-name>
      <last-name>Harris</last-name>
      <headline>Chief Executive Officer</headline>
    </person>
     ...
  </people>
  <num-results>108</num-results>
</people-search>
+1  A: 

http://nokogiri.org/ is an option you should investigate

Aaron Saunders
yeah I am familiar with Nokogiri, but it seemed like I couldn't specify the tag, that it "looped" and I had to count...so it wasn't behaving like an object...did I misunderstand that?
Angela
Read about XPath. It's extremely powerful, you should be able to pull out exactly what you want with it.
AboutRuby
A: 

nokogiri is a really nice xml parser for ruby that allows you to use xpath or css3 selectors to access your xml, but its not an xml to object mapper

there is a project called xml-mapping that does exactly this, by defining xpath expressions that should be mapped to object properties - and vice versa.

Nikolaus Gradwohl
hmmm...i wonder if nokogiri would work, could you give me an example using the type of xml that linkedin outputs?
Angela
A: 

This is how I did it for the Ruby Challenge using the built-in REXML.

This is basicaly the parsing code for the whole document:

doc = REXML::Document.new File.new cia_file
doc.elements.each('cia/continent') { |e| @continents.push Continent.new(e) }
doc.elements.each('cia/country') { |e| @countries.push Country.new(self, e) }
Dmytrii Nagirniak
+1  A: 

This might give you a jump start:

#!/usr/bin/env ruby

require 'nokogiri'

XML = %{<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<people-search>
  <people total="108" count="10" start="0">
    <person>
      <id>tePXJ3SX1o</id>
      <first-name>Bill</first-name>
      <last-name>Doe</last-name>
      <headline>Marketing Professional and Matchmaker</headline>
      <picture-url>http://media.linkedin.com:/foo.png&lt;/picture-url&gt;
    </person>
    <person>
      <id>pcfBxmL_Vv</id>
      <first-name>Ed</first-name>
      <last-name>Harris</last-name>
      <headline>Chief Executive Officer</headline>
    </person>
  </people>
  <num-results>108</num-results>
</people-search>}

doc = Nokogiri::XML(XML)

doc.search('//person').each do |person|
    firstname   = person.at('first-name').text
    puts "firstname: #{firstname}"
end
# >> firstname: Bill
# >> firstname: Ed

The idea is you're looping over the section that repeats, "person", in this case. Then you pick out the sections you want and extract the text. I'm using Nokogiri's .at() to get the first occurrence, but there are other ways to do it.

The Nokogiri site has good examples and well written documentation so be sure to spend a bit of time going over it. You should find it easy going.

Greg