To parse XML-documents, I recommend the gem nokogiri Show archive.org snapshot .
A few hints:
- 
xml = Nokogiri::XML("<list><item>foo</item><item>bar</item></list>")parses an xml string. You can also callNokogiri::HTMLto be more liberal about accepting invalid XML.
- 
xml / 'list item'returns all matching nodes;list itemis used like a CSS selector
- 
xml / './/list/item'also returns all matching nodes, but.//list/itemis now an XPath selector- XPath seems to be triggered by a leading .or/
 
- XPath seems to be triggered by a leading 
- 
xml % 'item'returns the first matching node
- 
node.attribute('foo')returns the attribute namedfoo
- 
node.attribute('foo').valuereturns its value
- 
node.contentreturns the content
Careful with XPath:
Whenever an XML document declares a namespace, like
<list xmlns="http://mylist.org'>
  <item />
</list>
xml % './/list' will not match any more (since there is no list tag any more, just a {http://mylist.org}:list tag).
You may use xml % './/xmlns:list' instead.
Posted by Tobias Kraze to makandra dev (2010-08-25 13:04)