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-3 of 3 total  RSS 

Count the no of elements in an XML node

This Ruby code count the no. of elements in an xml node by converting the children into an array and then returning the length.

require 'rexml/document'
include REXML

file = File.new('test.xml')
doc = Document.new(file)
puts doc.root.elements.to_a.length

Getting Results for Paged Display

We can present these blog entries a better way: as a page of formatted results with links to more
results. We can do that using Drupal’s pager. Let’s grab all of the blog entries again, only this
time we’ll display them as a paged result, with links to additional pages of results and “first and
last” links at the bottom of the page.

$sql = "SELECT * FROM {node} n WHERE type = 'blog' AND status = 1 ORDER BY
n.created DESC"
$result = pager_query(db_rewrite_sql($sql), 0, 10);
while ($data = db_fetch_object($result)) {
$node = node_load($data->nid);
print node_view($node, TRUE);
}
// Add links to remaining pages of results.
print theme('pager', NULL, 10);

Actionscript _XML Class

import _String;

dynamic class _XML extends XML {
	function _XML() {
		
	}
	
	public function $N(tag,xmlNode,xmlNodeArray){
		if (xmlNode == undefined) var xmlNode = this;
		if (xmlNodeArray == undefined) var xmlNodeArray:Array = new Array();
		var nodeArray:Array = new Array();
		
		for (var x=0; x<xmlNode.childNodes.length; x++) {
			if (xmlNode.childNodes[x].nodeType == 1){
				if (xmlNode.childNodes[x].nodeName == tag) xmlNodeArray.push(xmlNode.childNodes[x]);
				$N(tag,xmlNode.childNodes[x],xmlNodeArray);
			}
		}
		return xmlNodeArray;
	}
	
	// If multiple, returns a nodeValue Array
	// If single, returns a nodeValue String
	public function $V(tag,xmlNode) {
		if (xmlNode == undefined) var xmlNode = this;
		var n = $N(tag,xmlNode);
		if (n.length == 1){
			var nV = n[0].firstChild.nodeValue;
			if (nV != undefined){
				nV = escape(nV);
				nV = _String.Replace(nV,"%C2%93","%22");
				nV = _String.Replace(nV,"%C2%94","%22");
				nV = unescape(nV);
			}
			return nV;
		}
		else {
			var vArray:Array = new Array();
			for (var i:String in n) {
				vArray[i] = n[i].firstChild.nodeValue;
				if (vArray[i] != undefined){
					vArray[i] = escape(vArray[i]);
					vArray[i] = _String.Replace(vArray[i],"%C2%93","%22");
					vArray[i] = _String.Replace(vArray[i],"%C2%94","%22");
					vArray[i] = unescape(vArray[i]);
				}
			}
			return vArray;
		}
	}
}
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS