<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: elements code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 04:29:19 GMT</pubDate>
    <description>DZone Snippets: elements code</description>
    <item>
      <title>Adding helpful error messages to your Ruby code</title>
      <link>http://snippets.dzone.com/posts/show/5446</link>
      <description>This Ruby code raises an error if the XPath query fails because the attribute being queried did not exist for the given element. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def map_pattr(node, fieldx, valuex)&lt;br /&gt;    begin&lt;br /&gt;    parameter = node.root.elements["parameter[@field='#{fieldx}']"]&lt;br /&gt;    parameter.add_attribute('value', valuex)&lt;br /&gt;    parameter&lt;br /&gt;    &lt;br /&gt;    rescue&lt;br /&gt;      puts 'feedpopulated.rb: map_attr() the field ' + fieldx + ' was not found in params.'&lt;br /&gt;      raise&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Notice that a raise statement is used to ensure that the system error message is raised and any further code execution is halted.&lt;br /&gt;&lt;br /&gt;Without adding a customized helpful message I would be left scratching my head trying to work out what the following system error message meant.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;./feedpopulated.rb:27:in `map_pattr': undefined method `add_attribute' for nil:NilClass (NoMethodError)&lt;br /&gt;     from ./feedpopulated.rb:51:in `create_record'&lt;br /&gt;     from ./feedpopulated.rb:49:in `each'&lt;br /&gt;     from ./feedpopulated.rb:49:in `create_record'&lt;br /&gt;     from ./recordx.rb:91:in `call_create'&lt;br /&gt;     from ./s3fileuploader_handler.rb:14:in `call'&lt;br /&gt;     from ./s3fileuploader_handler.rb:40:in `invoke'&lt;br /&gt;     from ./uploadtwitteraudio.rb:22:in `initialize'&lt;br /&gt;     from /usr/lib/ruby/1.8/rexml/element.rb:890:in `each'&lt;br /&gt;     from /usr/lib/ruby/1.8/rexml/xpath.rb:53:in `each'&lt;br /&gt;     from /usr/lib/ruby/1.8/rexml/element.rb:890:in `each'&lt;br /&gt;     from ./uploadtwitteraudio.rb:18:in `initialize'&lt;br /&gt;     from ./uploadtwitteraudio.rb:72:in `new'&lt;br /&gt;     from ./uploadtwitteraudio.rb:72&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html"&gt;Programming Ruby: The Pragmatic Programmer's Guide - Exceptions, Catch, and Throw&lt;/a&gt; [ruby-doc.org]</description>
      <pubDate>Wed, 30 Apr 2008 11:40:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5446</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Count the no of elements in an XML node</title>
      <link>http://snippets.dzone.com/posts/show/4939</link>
      <description>This Ruby code count the no. of elements in an xml node by converting the children into an array and then returning the length.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'rexml/document'&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;file = File.new('test.xml')&lt;br /&gt;doc = Document.new(file)&lt;br /&gt;puts doc.root.elements.to_a.length&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 02 Jan 2008 00:32:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4939</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>getElementsAt //JavaScript Function</title>
      <link>http://snippets.dzone.com/posts/show/1479</link>
      <description>This function is able to get all the elements in a specific level of the array&lt;br /&gt;&lt;br /&gt;example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;x = [-1, [[8, 22, -7], 32, [[4, 10, -2], 122]], 13];&lt;br /&gt;alert(x.getElementsAt(3));&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;code:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com&lt;br /&gt;Array.prototype.getElementsAt = function(level){&lt;br /&gt;	var r = [], x = [], c = this, l = level, i;&lt;br /&gt;	do&lt;br /&gt;		if(i = c.length, !l)&lt;br /&gt;			for(; i; r[r.length] = c[--i]);&lt;br /&gt;		else&lt;br /&gt;			for(; i; c[--i] instanceof Array &amp;&amp; x.push(c[i], l - 1));&lt;br /&gt;	while(l = x.pop(), (c = x.pop()) != undefined);&lt;br /&gt;	return r;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 14 Feb 2006 16:20:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1479</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
  </channel>
</rss>
