Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Update a simple XML file using a generic update method.

This Ruby code updates an xml file using a method which can be applied where ever a simple struct form is used. ie. <records><record id="1"><name/><description/></record><record id="2"><name/><description/></record>


#test.xml = "<pears><pear id="1254"/><name/><region/></pear><pear id="5656"><name/><region/></pear></pears>"

require 'rexml/document'
include REXML

class Update
def initialize
end

def update_item(doc_p, pelement_name)
#get the target xml file
file = File.new('test.xml')
doc = Document.new(file)

puts doc
# get a reference to the record
id = doc_p.root.elements["parameter[@id]"].attributes.get_attribute('id')
puts id

node = doc.root.elements[pelement_name + "[@id='#{id}']"]
puts node
rnode = map_record(node, doc_p)
puts doc
end

def map_record(o_node, doc_input)
# set the element values for each field
doc_input.elements.each('parameters/parameter') do |in_node|
field = in_node.attributes.get_attribute('field').to_s
in_node.elements.each('attributes/attribute') do | attributex|
attribute_name = attributex.attributes.get_attribute('name').to_s
attribute_value = attributex.attributes.get_attribute('value').to_s
o_field = o_node.elements[attribute_name]
o_field.add_attribute(attribute_name, attribute_value)
end
map(o_node, field, in_node.attributes.get_attribute('value').to_s)

end
o_node
end

def map(o_node, field, value)
o_element = o_node.elements[field]
o_element.text = value
o_element
end


end

if __FILE__ == $0
u = Update.new
doc = Document.new
parameters = Element.new('parameters')
parameter = Element.new('parameter')
parameter.add_attribute('id', '5656')
parameters.add_element(parameter)
parameter = Element.new('parameter')
parameter.add_attribute('field', 'name')
parameter.add_attribute('value', 'Harovin Gaia')
parameters.add_element(parameter)
parameter = Element.new('parameter')
parameter.add_attribute('field', 'region')
parameter.add_attribute('value', 'Niagara')
parameters.add_element(parameter)
doc.add_element(parameters)
u.update_item(doc, 'pear')
puts doc
end

Generic XHTML template

I'm often having to reconstruct this, as I can no longer memorize everything (just blank HTML and BODY tags used to be acceptable ;-)) with the DOCTYPE and namespaces.. so to make it easier to grab in future:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title></title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
		<script type="text/javascript" src="common.js"></script>
	</head>

	<body>
		<div id="container">

			<div id="header">
			</div>

			<div id="wrapper">
				<div id="main">
					
				</div>
			</div>

			<div id="footer">
			</div>
			
		</div>
	</body>

</html>

Generic layout RHTML for Rails apps

This will not be 100% for everyone, but I use it for most of my apps and it does all the most important stuff I can't be bothered to retype each time.

(Updated August 2006 to slightly more up to date standards!)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title><%= @page_title %></title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<%= stylesheet_link_tag "main" %>
	<%= javascript_include_tag :defaults %>
</head>

<body<% if @page_class %> class="<%= @page_class %>"<% end %>>

	<% unless session[:user_id] %><%= render :partial => "generic/loginstuff" -%><% end %>

	<div id="container">
	
		<div id="header">
		</div>
	
		<% unless @message_override && @message_override == 1 %><%= render :partial => "generic/messageboxes" %><% end %>
	
		<div id="main">
			<%= yield %>
		</div>
	
		<div id="footer">
		</div>
	
	</div>

</body>
</html>

generic object

a generic object type thing for perl where you can use setanything and getanything to set and get properties via the AUTOLOAD method.
package thing;

use strict;

sub new {
	my %self;
	my ($class,@rest) = @_;
	unless ($#rest%2) { die "Odd parameter count\n"}
	for (my $k=0; $k<@rest; $k+=2) {
		my $wotsit = lc($rest[$k]);
		$wotsit =~ s/-//;
		$self{$wotsit} = $rest[$k+1];
		}
	bless \%self,$class;
}

sub AUTOLOAD {
        my ($where,$val) = @_;
        our $AUTOLOAD;
        $AUTOLOAD =~ s/.*://;
        my ($dirn,$what) = ($AUTOLOAD =~ /(...)(.*)/);
        if ($dirn eq "get") {
                return $$where{lc($what)};
        } elsif ($dirn eq "set") {
                $$where{lc($what)} = $val;
        }
        return 1;
}

1;



Here's a quick example of usage
use thing

$house = new thing("type","building","rooms",12,"foundations", "yes");

print $house -> gettype();
$house -> settype("tree");
print " " . $house -> gettype();

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS