<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rexml code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 12 May 2008 09:26:23 GMT</pubDate>
    <description>DZone Snippets: rexml code</description>
    <item>
      <title>Using ruby to upload multiple files to Amazon S3</title>
      <link>http://snippets.dzone.com/posts/show/5431</link>
      <description>This ruby code builds upon the code &lt;a href="http://snippets.dzone.com/posts/show/3062"&gt;S3 upload client for Ruby&lt;/a&gt; [dzone.com] by reading the account details and file details from XML files.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;#file : rubys3file2upload.rb&lt;br /&gt;&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'aws/s3'&lt;br /&gt;require 'rexml/document'&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;class S3FileUpload&lt;br /&gt;&lt;br /&gt;  def initialize(xml_accounts_file, xml_upload_file)&lt;br /&gt;    initialize_account(xml_accounts_file)&lt;br /&gt;    main(xml_upload_file)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def initialize_account(accounts_file)&lt;br /&gt;&lt;br /&gt;    file = File.new(accounts_file)&lt;br /&gt;    doc = Document.new(file)&lt;br /&gt;&lt;br /&gt;    account = doc.root.elements['records/access']&lt;br /&gt;    h = Hash.new&lt;br /&gt;    h[:access_key_id] = account.elements['key_id'].text.to_s&lt;br /&gt;    h[:secret_access_key] = account.elements['secret'].text.to_s&lt;br /&gt;    AWS::S3::Base.establish_connection!(h)&lt;br /&gt;&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def main(xml_upload_file)&lt;br /&gt;    file = File.new(xml_upload_file)&lt;br /&gt;    doc = Document.new(file)&lt;br /&gt;puts doc&lt;br /&gt;    doc.root.elements.each('records/file') do |f|&lt;br /&gt;      local_file = f.elements['local'].text.to_s&lt;br /&gt;      bucket = f.elements['bucket'].text.to_s&lt;br /&gt;      mime_type = f.elements['mime_type'].text.to_s&lt;br /&gt;      upload(local_file, mime_type, bucket)&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def upload(local_file, mime_type, bucket)&lt;br /&gt;&lt;br /&gt;    base_name = File.basename(local_file)&lt;br /&gt;&lt;br /&gt;    puts "Uploading #{local_file} as '#{base_name}' to '#{bucket}'"&lt;br /&gt;&lt;br /&gt;    AWS::S3::S3Object.store(&lt;br /&gt;      base_name,&lt;br /&gt;      File.open(local_file),&lt;br /&gt;      bucket,&lt;br /&gt;      :content_type =&gt; mime_type,&lt;br /&gt;      :access =&gt; :public_read&lt;br /&gt;    )&lt;br /&gt;&lt;br /&gt;    puts "Uploaded!"&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0&lt;br /&gt;  s3fu = S3FileUpload.new('s3accounts.xml', 's3files2upload.xml')&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: s3accounts.xml&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;s3accounts&gt;&lt;br /&gt;  &lt;summary/&gt;&lt;br /&gt;  &lt;records&gt;&lt;br /&gt;    &lt;access id="100"&gt;&lt;name&gt;REPLACE_ME&lt;/name&gt;&lt;key_id&gt;REPLACE_ME&lt;/key_id&gt;&lt;secret&gt;REPLACE_ME&lt;/secret&gt;&lt;/access&gt;&lt;br /&gt;  &lt;/records&gt;&lt;br /&gt;&lt;/s3accounts&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: s3files2upload.xml&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;s3files2upload&gt;&lt;br /&gt;  &lt;summary/&gt;&lt;br /&gt;  &lt;records&gt;&lt;br /&gt;    &lt;file&gt;&lt;br /&gt;      &lt;local&gt;autocomplete.html&lt;/local&gt;&lt;br /&gt;      &lt;bucket&gt;t2000&lt;/bucket&gt;&lt;br /&gt;      &lt;mime_type&gt;text/html&lt;/mime_type&gt;&lt;br /&gt;    &lt;/file&gt;&lt;br /&gt;    &lt;file&gt;&lt;br /&gt;      &lt;local&gt;autocomplete2.html&lt;/local&gt;&lt;br /&gt;      &lt;bucket&gt;t2000&lt;/bucket&gt;&lt;br /&gt;      &lt;mime_type&gt;text/html&lt;/mime_type&gt;&lt;br /&gt;    &lt;/file&gt;&lt;br /&gt;    &lt;file&gt;&lt;br /&gt;      &lt;local&gt;autocomplete3.html&lt;/local&gt;&lt;br /&gt;      &lt;bucket&gt;t2000&lt;/bucket&gt;&lt;br /&gt;      &lt;mime_type&gt;text/html&lt;/mime_type&gt;&lt;br /&gt;    &lt;/file&gt;&lt;br /&gt;  &lt;/records&gt;&lt;br /&gt;&lt;/s3files2upload&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note: The file given the correct permission could be read from http://t2000.s3.amazonaws.com/autocomplete.html&lt;br /&gt;&lt;br /&gt;Reference: http://amazon.rubyforge.org/</description>
      <pubDate>Sat, 26 Apr 2008 14:17:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5431</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Converting text to SVG</title>
      <link>http://snippets.dzone.com/posts/show/5388</link>
      <description>First of all the Ruby code reads the text, splits it into an array and then saves it in XML format.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'rexml/document'&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;a = "Open source is a development method for software that harnesses the power of distributed peer &lt;br /&gt;review and transparency of process. The promise of open source is better quality, higher reliability, &lt;br /&gt;more flexibility, lower cost, and an end to predatory vendor lock-in.".split(' ') &lt;br /&gt;&lt;br /&gt;doc = Document.new&lt;br /&gt;doc.add_element('text')&lt;br /&gt;oline = Element.new('line')&lt;br /&gt;&lt;br /&gt;char_count = 0&lt;br /&gt;a.each do |word|&lt;br /&gt;  &lt;br /&gt;  oword = Element.new('word')&lt;br /&gt;  oword.text = word.to_s&lt;br /&gt;  oword.add_attribute('length',char_count)&lt;br /&gt;  oline &lt;&lt; oword &lt;br /&gt;  char_count += word.length&lt;br /&gt;  &lt;br /&gt;  if char_count &gt; 50&lt;br /&gt;    doc.root &lt;&lt; oline&lt;br /&gt;    oline = Element.new('line')&lt;br /&gt;    char_count = 0&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;doc.root &lt;&lt; oline&lt;br /&gt;puts char_count&lt;br /&gt;puts doc&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Here's a sample of the XML created&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;text&gt;&lt;br /&gt;  &lt;line&gt;&lt;br /&gt;    &lt;word length='0'&gt;Open&lt;/word&gt;&lt;word length='4'&gt;source&lt;/word&gt;&lt;word length='10'&gt;is&lt;/word&gt;&lt;br /&gt;    &lt;word length='12'&gt;a&lt;/word&gt;&lt;word length='13'&gt;development&lt;/word&gt;&lt;word length='24'&gt;method&lt;/word&gt;&lt;br /&gt;    &lt;word length='30'&gt;for&lt;/word&gt;&lt;word length='33'&gt;software&lt;/word&gt;&lt;word length='41'&gt;that&lt;/word&gt;&lt;br /&gt;    &lt;word length='45'&gt;harnesses&lt;/word&gt;&lt;br /&gt;  &lt;/line&gt;&lt;br /&gt;  &lt;line&gt;&lt;br /&gt;    &lt;word length='0'&gt;the&lt;/word&gt;&lt;word length='3'&gt;power&lt;/word&gt;&lt;br /&gt;    &lt;word length='8'&gt;of&lt;/word&gt;&lt;word length='10'&gt;distributed&lt;/word&gt;&lt;word length='21'&gt;peer&lt;/word&gt;&lt;br /&gt;    &lt;word length='25'&gt;review&lt;/word&gt;&lt;word length='31'&gt;and&lt;/word&gt;&lt;word length='34'&gt;transparency&lt;/word&gt;&lt;br /&gt;    &lt;word length='46'&gt;of&lt;/word&gt;&lt;word length='48'&gt;process.&lt;/word&gt;&lt;br /&gt;  &lt;/line&gt;&lt;br /&gt;  &lt;line&gt;&lt;br /&gt;    &lt;word length='0'&gt;The&lt;/word&gt;&lt;br /&gt;    &lt;word length='3'&gt;promise&lt;/word&gt;&lt;word length='10'&gt;of&lt;/word&gt;&lt;word length='12'&gt;open&lt;/word&gt;&lt;br /&gt;    &lt;word length='16'&gt;source&lt;/word&gt;&lt;word length='22'&gt;is&lt;/word&gt;&lt;word length='24'&gt;better&lt;/word&gt;&lt;br /&gt;    &lt;word length='30'&gt;quality,&lt;/word&gt;&lt;word length='38'&gt;higher&lt;/word&gt;&lt;word length='44'&gt;reliability,&lt;/word&gt;&lt;br /&gt;  &lt;/line&gt;&lt;br /&gt;  &lt;line&gt;&lt;br /&gt;    &lt;word length='0'&gt;more&lt;/word&gt;&lt;word length='4'&gt;flexibility,&lt;/word&gt;&lt;word length='16'&gt;lower&lt;/word&gt;&lt;br /&gt;    &lt;word length='21'&gt;cost,&lt;/word&gt;&lt;word length='26'&gt;and&lt;/word&gt;&lt;word length='29'&gt;an&lt;/word&gt;&lt;br /&gt;    &lt;word length='31'&gt;end&lt;/word&gt;&lt;word length='34'&gt;to&lt;/word&gt;&lt;word length='36'&gt;predatory&lt;/word&gt;&lt;br /&gt;    &lt;word length='45'&gt;vendor&lt;/word&gt;&lt;br /&gt;  &lt;/line&gt;&lt;br /&gt;  &lt;line&gt;&lt;br /&gt;    &lt;word length='0'&gt;lock-in.&lt;/word&gt;&lt;br /&gt;  &lt;/line&gt;&lt;br /&gt;&lt;/text&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;The XML is then transformed to SVG using the following XSL file&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;br /&gt;&lt;br /&gt;&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;&lt;br /&gt;&lt;br /&gt;&lt;xsl:output encoding="UTF-8"&lt;br /&gt;            method="xml"&lt;br /&gt;            indent="yes"/&gt;&lt;br /&gt;            &lt;br /&gt;  &lt;xsl:template match="text"&gt;&lt;br /&gt;    &lt;svg xmlns="http://www.w3.org/2000/svg" width="100%"&lt;br /&gt;                  xmlns:xlink="http://www.w3.org/1999/xlink" &gt;&lt;br /&gt;      &lt;g id="sketch" class="sketch"&gt;&lt;br /&gt;        &lt;xsl:apply-templates select="line"/&gt;&lt;br /&gt;      &lt;/g&gt;&lt;br /&gt;    &lt;/svg&gt;&lt;br /&gt;  &lt;/xsl:template&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;xsl:template match="line"&gt;&lt;br /&gt;    &lt;xsl:variable name="xfactor"&gt;12&lt;/xsl:variable&gt;&lt;br /&gt;    &lt;xsl:variable name="yfactor"&gt;20&lt;/xsl:variable&gt;&lt;br /&gt;    &lt;xsl:variable name="pos" select="position()"&gt;&lt;/xsl:variable&gt;&lt;br /&gt;    &lt;xsl:apply-templates select="word"&gt;&lt;br /&gt;      &lt;xsl:with-param name="xfactor" select="$xfactor"&gt;&lt;/xsl:with-param&gt;&lt;br /&gt;      &lt;xsl:with-param name="y" select="$pos * $yfactor + 10"&gt;&lt;/xsl:with-param&gt;&lt;br /&gt;    &lt;/xsl:apply-templates&gt;&lt;br /&gt;  &lt;/xsl:template&gt;&lt;br /&gt;&lt;br /&gt;  &lt;xsl:template match="word"&gt;&lt;br /&gt;    &lt;xsl:param name="xfactor"/&gt;&lt;br /&gt;    &lt;xsl:param name="y"/&gt;&lt;br /&gt;    &lt;text xmlns="http://www.w3.org/2000/svg" font-size="12pt" x="{@length * $xfactor +10}" y="{$y}" id="t1"&gt;&lt;xsl:value-of select="."/&gt;&lt;/text&gt;&lt;br /&gt;  &lt;/xsl:template&gt;&lt;br /&gt;&lt;/xsl:stylesheet&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;The final &lt;a href="http://www.twitxr.com/image/31865/"&gt;SVG output&lt;/a&gt; [twitxr.com] shows 5 lines of text with each word as a separate SVG text element.</description>
      <pubDate>Fri, 18 Apr 2008 18:14:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5388</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Append an XML element using the append operator</title>
      <link>http://snippets.dzone.com/posts/show/5382</link>
      <description>This Ruby code appends an XML node to an XML document using the append &lt;&lt; operator. &lt;br /&gt;&lt;code&gt;&lt;br /&gt;      doc2 = Document.new(xml_svg)&lt;br /&gt;      obody.text = ''&lt;br /&gt;      obody &lt;&lt; doc2.root&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;In this example the original obody.text contained escaped xml, which was then initialised as an XML document and appended to obody to be processed as pure XML.</description>
      <pubDate>Thu, 17 Apr 2008 23:50:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5382</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Deleting a redundant shape message from the whiteboard queue</title>
      <link>http://snippets.dzone.com/posts/show/5325</link>
      <description>This code is used to remove a message from the whiteboard queue containing a shape which has recently moved it's position (x and y coordinates) on the board. Code extract from whiteboardqueue.rb&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def call_delete_shape(params)&lt;br /&gt;    #get the shape's message id.&lt;br /&gt;    doc = Document.new(params)&lt;br /&gt;    shape_id = doc.root.elements["param[@var='id']"].text.to_s&lt;br /&gt;    initialize_doc&lt;br /&gt;&lt;br /&gt;    doc_xml = Document.new(decode2(@doc_file.root.to_s))&lt;br /&gt;    id = doc_xml.root.elements["records/message/body/*[@id='#{shape_id}']"].parent.parent.attributes.get_attribute('id')&lt;br /&gt;    delete_record(id)&lt;br /&gt;    save_file&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;*update 10:04pm*&lt;br /&gt;Added the link to the project code http://github.com/jrobertson/whiteboardqueue/tree</description>
      <pubDate>Sat, 05 Apr 2008 19:39:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5325</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Save a Ruby source text file to XML.</title>
      <link>http://snippets.dzone.com/posts/show/5163</link>
      <description>This code will output a text file as an XML file which can later be transformed into an HTML file using a back-end XSLT processor called Gorg.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby &lt;br /&gt;&lt;br /&gt;#file: rubytxt2xml.rb&lt;br /&gt;&lt;br /&gt;require 'rexml/document'&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;class RubyTxt2XML&lt;br /&gt;  &lt;br /&gt;  def rubytxt2xml(h)&lt;br /&gt;    h[:infilepath] = './' if h[:infilepath].nil?&lt;br /&gt;    h[:outfilepath] = './' if h[:outfilepath].nil?&lt;br /&gt;    h[:xmlfile] = h[:sourcefile][/^(.*)\.\w+$/,1] + '.xml' if h[:xmlfile].nil?&lt;br /&gt;    buffer = File.new(h[:infilepath] + h[:sourcefile],'r').read&lt;br /&gt;    &lt;br /&gt;    doc = Document.new&lt;br /&gt;    doc.add_element('ruby_txt')&lt;br /&gt;    body = Element.new('source')&lt;br /&gt;    body.text = CData.new(buffer)&lt;br /&gt;    doc.root.add_element(body)&lt;br /&gt;    &lt;br /&gt;    file = File.new(h[:outfilepath] + h[:xmlfile],'w')&lt;br /&gt;    file.puts doc&lt;br /&gt;    file.close&lt;br /&gt;    &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0&lt;br /&gt;  rt2x = RubyTxt2XML.new&lt;br /&gt;  rt2x.rubytxt2xml(:sourcefile  =&gt; 'rubytxt2xml.rb')&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;ruby_txt&gt;&lt;br /&gt;  &lt;source&gt;&lt;br /&gt;    &lt;![CDATA[&lt;br /&gt;      #!/usr/bin/ruby &lt;br /&gt;&lt;br /&gt;      #file: rubytxt2xml.rb&lt;br /&gt;&lt;br /&gt;      require 'rexml/document'&lt;br /&gt;      include REXML&lt;br /&gt;&lt;br /&gt;      class RubyTxt2XML&lt;br /&gt;        &lt;br /&gt;        def rubytxt2xml(h)&lt;br /&gt;          h[:infilepath] = './' if h[:infilepath].nil?&lt;br /&gt;          h[:outfilepath] = './' if h[:outfilepath].nil?&lt;br /&gt;          h[:xmlfile] = h[:sourcefile][/^(.*)\.\w+$/,1] + '.xml' if h[:xmlfile].nil?&lt;br /&gt;          buffer = File.new(h[:infilepath] + h[:sourcefile],'r').read&lt;br /&gt;          &lt;br /&gt;          doc = Document.new&lt;br /&gt;          doc.add_element('ruby_txt')&lt;br /&gt;          body = Element.new('source')&lt;br /&gt;          body.text = CData.new(buffer)&lt;br /&gt;          doc.root.add_element(body)&lt;br /&gt;          &lt;br /&gt;          file = File.new(h[:outfilepath] + h[:xmlfile],'w')&lt;br /&gt;          file.puts doc&lt;br /&gt;          file.close&lt;br /&gt;          &lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;      if __FILE__ == $0&lt;br /&gt;        rt2x = RubyTxt2XML.new&lt;br /&gt;        rt2x.rubytxt2xml(:sourcefile  =&gt; 'rubytxt2xml.rb')&lt;br /&gt;      end&lt;br /&gt;    ]]&gt;&lt;br /&gt;  &lt;/source&gt;&lt;br /&gt;&lt;/ruby_txt&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 22 Feb 2008 15:18:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5163</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>ProjectX client-side code</title>
      <link>http://snippets.dzone.com/posts/show/5150</link>
      <description>This Ruby code uses a unified XML format to create a password record on a web server. It's intended to be run as a batch file which gets called from another Ruby application called maintain_projectx which gets called from a cronjob.&lt;br /&gt;&lt;br /&gt;In this example the password is stored on the server not for authentication but simply to provide a reminder service in the event the user forgets it.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'net/http'&lt;br /&gt;require 'rexml/document'&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;class ProjectXClient&lt;br /&gt;  attr :doc&lt;br /&gt;  def initialize(raw_url)&lt;br /&gt;    url = URI.escape(raw_url)&lt;br /&gt;    xml_data = Net::HTTP.get_response(URI.parse(url)).body&lt;br /&gt;    @doc = Document.new(xml_data)&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0&lt;br /&gt;&lt;br /&gt;  xml_project = &lt;&lt;PROJECT&lt;br /&gt;  &lt;project name='password'&gt;&lt;br /&gt;    &lt;method name='create'&gt;&lt;br /&gt;      &lt;params&gt;&lt;br /&gt;        &lt;param var='password' val='p6789c'/&gt;&lt;br /&gt;        &lt;param var='title' val='hotmail'/&gt;&lt;br /&gt;      &lt;/params&gt;&lt;br /&gt;    &lt;/method&gt;&lt;br /&gt;  &lt;/project&gt;&lt;br /&gt;PROJECT&lt;br /&gt;  &lt;br /&gt;  pxc = ProjectXClient.new("http://yourdomain.com/p/projectx.cgi?xml_project=" + xml_project)&lt;br /&gt;  doc = pxc.doc&lt;br /&gt;  puts doc&lt;br /&gt;    &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output -what's returned from the server is an XML response containing a result. The result echos the method executed and the output from that method, which in this instance is the xml record node 'entry'.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;result method='rtn_create'&gt;&lt;br /&gt;  &lt;append id='19367'&gt;&lt;br /&gt;    &lt;entry id='19367'&gt;&lt;br /&gt;      &lt;password&gt;p6789c&lt;/password&gt;&lt;br /&gt;      &lt;title&gt;hotmail&lt;/title&gt;&lt;br /&gt;      &lt;description/&gt;&lt;br /&gt;    &lt;/entry&gt;&lt;br /&gt;  &lt;/append&gt;&lt;br /&gt;&lt;/result&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 18 Feb 2008 18:49:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5150</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Scrape an XHTML document using Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5039</link>
      <description>A simple Ruby script to scrape an XHTML file with the selected content being saved to an xml file ready for transformation into an RSS feed.  This example uses the XHTML file from http://newsgang.net/audio/ which is then saved locally as 'thegang.xml'.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;# file: thegang.rb&lt;br /&gt;&lt;br /&gt;require 'rexml/document'&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;class TheGang&lt;br /&gt;  def initialize()&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def rssify()&lt;br /&gt;    file = File.new('thegang.xml','r')&lt;br /&gt;    doc = Document.new(file)&lt;br /&gt;    rss_doc = Document.new&lt;br /&gt;    root = Element.new('rss')&lt;br /&gt;    rss_doc.add_element(root)&lt;br /&gt;    &lt;br /&gt;    doc.root.elements.each("body/div/ul/li/h2/a") do |node|    &lt;br /&gt;      o_rssitem = Element.new('item')&lt;br /&gt;      o_li = node.parent.parent&lt;br /&gt;      &lt;br /&gt;      o_rsstitle = Element.new('title')&lt;br /&gt;      o_rsstitle.text = node.text.gsub(/[\n,' ']/,'')&lt;br /&gt;      o_rssitem.add_element(o_rsstitle)&lt;br /&gt;      &lt;br /&gt;      o_rsshref_audio = Element.new('href_audio')&lt;br /&gt;      o_rsshref_audio.text = node.attributes.get_attribute('href').to_s.gsub('amp;&amp;','')      &lt;br /&gt;      o_rssitem.add_element(o_rsshref_audio)&lt;br /&gt;      &lt;br /&gt;      o_rsshref = Element.new('href')&lt;br /&gt;      o_rsshref.text = o_rsshref_audio.text.gsub('&amp;amp;from=audio','')      &lt;br /&gt;      o_rssitem.add_element(o_rsshref)&lt;br /&gt;      &lt;br /&gt;      o_rssdate = Element.new('date')&lt;br /&gt;      o_rssdate.text = "#{o_li.elements["p/span[1]"].text} #{o_li.elements["p/span[2]"].text}"&lt;br /&gt;      o_rssitem.add_element(o_rssdate)&lt;br /&gt;      rss_doc.root.add_element(o_rssitem)&lt;br /&gt;      &lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    file = File.new('thegang_rss.xml','w')&lt;br /&gt;    file.puts rss_doc&lt;br /&gt;    file.close&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0&lt;br /&gt;  gang = TheGang.new&lt;br /&gt;  gang.rssify&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;see also: www.dapper.net&lt;br /&gt;&lt;br /&gt;output (extract)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;rss&gt;&lt;br /&gt;  &lt;item&gt;&lt;title&gt;TheGangXII-II&lt;/title&gt;&lt;href_audio&gt;/gangitem/id=6501&amp;amp;from=audio&lt;/href_audio&gt;&lt;href&gt;/gangitem/id=6501&lt;/href&gt;&lt;date&gt;Jan 25&lt;/date&gt;&lt;/item&gt;&lt;br /&gt;  &lt;item&gt;&lt;title&gt;TheGangXII-I&lt;/title&gt;&lt;href_audio&gt;/gangitem/id=6499&amp;amp;from=audio&lt;/href_audio&gt;&lt;href&gt;/gangitem/id=6499&lt;/href&gt;&lt;date&gt;Jan 25&lt;/date&gt;&lt;/item&gt;&lt;br /&gt;  &lt;item&gt;&lt;title&gt;NewsGangLive01.24.08&lt;/title&gt;&lt;href_audio&gt;/gangitem/id=6445&amp;amp;from=audio&lt;/href_audio&gt;&lt;href&gt;/gangitem/id=6445&lt;/href&gt;&lt;date&gt;Jan 24&lt;/date&gt;&lt;/item&gt;&lt;br /&gt;  &lt;item&gt;&lt;title&gt;NewsGangLiveII&lt;/title&gt;&lt;href_audio&gt;/gangitem/id=6377&amp;amp;from=audio&lt;/href_audio&gt;&lt;href&gt;/gangitem/id=6377&lt;/href&gt;&lt;date&gt;Jan 23&lt;/date&gt;&lt;/item&gt;&lt;br /&gt;  ...&lt;br /&gt;&lt;/rss&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 27 Jan 2008 14:09:24 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5039</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Automate the insertion of text into a file.</title>
      <link>http://snippets.dzone.com/posts/show/5032</link>
      <description>This example updates an xsl file with a new xsl:include declaration, and with a javascript header declaration. It reads 'guide.xsl' as a text file, and reads the template 'guide_ptemplate.xml' as a rexml document. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;#file: create add2guide_xsl.rb&lt;br /&gt;&lt;br /&gt;require 'rexml/document'&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;class Add2GuideTxt&lt;br /&gt;  def initialize&lt;br /&gt;    @guide = 'guide.xsl'&lt;br /&gt;    @guide_template = 'guide_ptemplate.xml'&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def insertText(project)&lt;br /&gt;    # read guide.txt and return the buffer&lt;br /&gt;    buffer = readGuide(@guide)    &lt;br /&gt;    replaceBuffer(project, buffer, @guide) if not buffer.match("&lt;xsl:include href='/xsl/#{project}.xsl'/&gt;")&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def replaceBuffer(project, buffer, output_file)&lt;br /&gt;    # read guide_ptemplate.xsl and return the doc&lt;br /&gt;    doc = readGuideTemplate(@guide_template)    &lt;br /&gt;    # read the xsl:include&lt;br /&gt;    xsl_include = buildIncludeTemplate(project, doc)&lt;br /&gt;    # read the xsl:if&lt;br /&gt;    xsl_if = buildIfTemplate(project,doc)&lt;br /&gt;    &lt;br /&gt;    eoi = "&lt;!-- &lt;/xsl_includes&gt; --&gt;"&lt;br /&gt;    buffer = buffer.gsub(eoi, "#{xsl_include}\n" + eoi)&lt;br /&gt;    eoj = "&lt;!--&lt;/xsl_javascript&gt; --&gt;"&lt;br /&gt;    buffer = buffer.gsub(eoj, "\n#{xsl_if}\n" + eoj)&lt;br /&gt;    &lt;br /&gt;    file = File.new(output_file,'w')&lt;br /&gt;    file.puts buffer.gsub("&amp;quot;","'")&lt;br /&gt;    file.close&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def buildIncludeTemplate(project, doc)&lt;br /&gt;    xsl_include = doc.root.elements['xsl:include']&lt;br /&gt;    xsl_include.attributes['href'] = '/xsl/' + project + '.xsl'&lt;br /&gt;    xsl_include&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def buildIfTemplate(project, doc)&lt;br /&gt;    xsl_if = doc.root.elements['xsl:if']&lt;br /&gt;    xsl_if.attributes['test'] = project&lt;br /&gt;    xsl_if.elements['script[3]'].text = 'const ProjectX = "' + project + '";'&lt;br /&gt;    xsl_if&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def readGuide(filename) &lt;br /&gt;    file = File.new(filename,'r')&lt;br /&gt;    buffer = file.read&lt;br /&gt;    file.close&lt;br /&gt;    buffer&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def readGuideTemplate(filename) &lt;br /&gt;    file = File.new(filename)&lt;br /&gt;    Document.new(file)&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0&lt;br /&gt;  a2g = Add2GuideTxt.new&lt;br /&gt;  #add the new project to the guide.xsl file  &lt;br /&gt;  a2g.insertText('tasks')&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;file: guide.xsl (target file)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!-- &lt;xsl_includes&gt; --&gt;&lt;br /&gt;&lt;br /&gt;&lt;!-- &lt;xsl_car_log.xsl/&gt; --&gt;&lt;br /&gt;&lt;xsl:include href="/xsl/car_log.xsl"/&gt;&lt;br /&gt;&lt;!-- &lt;/xsl_includes&gt; --&gt;&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;&lt;xsl:if test="car_logpage|car_log"&gt;&lt;br /&gt;  &lt;script type="text/javascript" src="/p/js/edit_recordx.js"&gt;&lt;/script&gt;&lt;br /&gt;  &lt;script type="text/javascript" src="/p/js/itasks5.js"&gt;&lt;/script&gt;&lt;br /&gt;  &lt;script type="text/javascript"&gt;const ProjectX = "car_log";&lt;/script&gt;&lt;br /&gt;&lt;/xsl:if&gt;&lt;br /&gt;&lt;!--&lt;/xsl_javascript&gt; --&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;file: guide_ptemplate.xml (template used to insert blocks of code into guide.xsl)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;template&gt;&lt;br /&gt;  &lt;xsl:include href="[x]"/&gt;&lt;br /&gt;  &lt;xsl:if test="[x]"&gt;&lt;br /&gt;    &lt;script type="text/javascript" src="/p/js/edit_recordx.js"&gt;&lt;/script&gt;&lt;br /&gt;    &lt;script type="text/javascript" src="/p/js/itasks5.js"&gt;&lt;/script&gt;&lt;br /&gt;    &lt;script type="text/javascript"&gt;[x]&lt;/script&gt;&lt;br /&gt;  &lt;/xsl:if&gt;&lt;br /&gt;&lt;/template&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 23 Jan 2008 15:19:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5032</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Unify the handling of XML records</title>
      <link>http://snippets.dzone.com/posts/show/4833</link>
      <description>This Ruby code creates, updates or deletes an XML record, using a hash, record handling objects, and XML to invoke the correct method.  &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#file: recordx_handler.rb&lt;br /&gt;require 'recordx'&lt;br /&gt;&lt;br /&gt;class RecordX_Update &lt; RecordX&lt;br /&gt;  def call(params)&lt;br /&gt;    #todo: write the code to read the xml parameter string&lt;br /&gt;    update_record(h)&lt;br /&gt;    save_file &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class RecordX_Create &lt; RecordX&lt;br /&gt;  def call(params)&lt;br /&gt;    #todo: write the code to read the xml parameter string  &lt;br /&gt;    create_record(h)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class RecordX_Delete &lt; RecordX&lt;br /&gt;  def call(params)&lt;br /&gt;    doc = Document.new(params)&lt;br /&gt;    node = doc.root.elements["param[@var='id']"]&lt;br /&gt;    puts node&lt;br /&gt;    id = node.attributes.get_attribute('val').to_s&lt;br /&gt;    delete_record(id)&lt;br /&gt;    puts 'deleted record ' + id&lt;br /&gt;    save_file&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class RecordX_handler&lt;br /&gt;  def invoke(method, params)&lt;br /&gt;    h = Hash.new&lt;br /&gt;    h["create"] = RecordX_Create.new&lt;br /&gt;    h["update"] = RecordX_Update.new&lt;br /&gt;    h["delete"] = RecordX_Delete.new&lt;br /&gt;    h[method].call(params)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0&lt;br /&gt;  xml_method = "&lt;method name='delete'&gt;&lt;params&gt;&lt;param var='id' val='17648' /&gt;&lt;/params&gt;&lt;/method&gt;"&lt;br /&gt;  doc = Document.new(xml_method)&lt;br /&gt;  method = doc.root.attributes.get_attribute('name').to_s&lt;br /&gt;  params = doc.root.elements['params'].to_s&lt;br /&gt;&lt;br /&gt;  rh = RecordX_handler.new&lt;br /&gt;  rh.invoke(method, params)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This code is intended to be called by a Ruby CGI script which can simply relay the cgi post argument to the recordx_handler object.&lt;br /&gt;</description>
      <pubDate>Sun, 02 Dec 2007 13:50:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4833</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Deleting an xml node from a REXML::document</title>
      <link>http://snippets.dzone.com/posts/show/4574</link>
      <description>// Reads an XML file, then deletes the root element's 1st child element.&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('journal250907.xml')&lt;br /&gt;doc = Document.new(file)&lt;br /&gt;&lt;br /&gt;puts doc&lt;br /&gt;&lt;br /&gt;o_element = Element.new('abc')&lt;br /&gt;o_element.text = "123"&lt;br /&gt;&lt;br /&gt;o_node = doc.elements['journal/entry']&lt;br /&gt;&lt;br /&gt;o_node.parent.delete(o_node)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 26 Sep 2007 18:13:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4574</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
  </channel>
</rss>
