<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: dom code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 16 May 2008 16:38:38 GMT</pubDate>
    <description>DZone Snippets: dom code</description>
    <item>
      <title>Generating Taconite command documents</title>
      <link>http://snippets.dzone.com/posts/show/5475</link>
      <description>// description of your code here&lt;br /&gt;Hello,&lt;br /&gt;&lt;br /&gt;This is a port of a php class used to generate XML taconite command documents, useful for (very) easy and powerful ajaxy stuff, if you don't know what that is just check it there in french : http://www.desfrenes.com/playground/taconite/ or there in english : http://www.malsup.com/jquery/taconite/.&lt;br /&gt;&lt;br /&gt;Basically what it does is generate an XML document that is later processed by a javascript plugin which executes a serie of DOM modifications.&lt;br /&gt;&lt;br /&gt;About the code, I'm a Django beginner as well as a Python beginner so kind advices are welcome.&lt;br /&gt;&lt;br /&gt;Cheers. &lt;br /&gt;&lt;code&gt;&lt;br /&gt;# usage:&lt;br /&gt;#&lt;br /&gt;# t = Taconite()&lt;br /&gt;#&lt;br /&gt;# t.append("#toto","&lt;label&gt;test&lt;/label&gt;")&lt;br /&gt;# t.remove("#tutu")&lt;br /&gt;# t.js('alert("hello world");')&lt;br /&gt;# t.toggleClass('blue','body')&lt;br /&gt;# t.css("body","background-color","white")&lt;br /&gt;# [...]&lt;br /&gt;# print t.toprettyxml()&lt;br /&gt;&lt;br /&gt;import xml.dom.minidom as dom&lt;br /&gt;&lt;br /&gt;class Taconite(dom.Document):&lt;br /&gt;    def __init__(self):&lt;br /&gt;        dom.Document.__init__(self)&lt;br /&gt;        taconite = self.createElement("taconite")&lt;br /&gt;        self.appendChild(taconite)&lt;br /&gt;&lt;br /&gt;    def __str__(self):&lt;br /&gt;        return self.toxml(encoding="utf-8")&lt;br /&gt;    &lt;br /&gt;    def camelizeCssProperty(self,property):&lt;br /&gt;        words = property.split('-')&lt;br /&gt;        camelized = words[0].lower()&lt;br /&gt;        for word in words[1:] :&lt;br /&gt;            camelized = camelized + word[0].upper() + word[1:]&lt;br /&gt;        return camelized&lt;br /&gt;    &lt;br /&gt;    def js(self,script):&lt;br /&gt;        command = self.createElement("eval")&lt;br /&gt;        js = self.createTextNode(script)&lt;br /&gt;        command.appendChild(js)&lt;br /&gt;        self.childNodes[0].appendChild(command)&lt;br /&gt;    &lt;br /&gt;    def changeContentCommand(self,method,selector,content):&lt;br /&gt;        html_dom = dom.parseString(content)&lt;br /&gt;        command = self.createElement(method)&lt;br /&gt;        command.setAttribute("select",selector)&lt;br /&gt;        command.appendChild(html_dom.childNodes[0])&lt;br /&gt;        self.childNodes[0].appendChild(command)&lt;br /&gt;    &lt;br /&gt;    def changeStateCommand(self,action,selector):&lt;br /&gt;        command = self.createElement(action)&lt;br /&gt;        command.setAttribute("select",selector)&lt;br /&gt;        self.childNodes[0].appendChild(command)&lt;br /&gt;    &lt;br /&gt;    def CssCommand(self,action,css_class,selector):&lt;br /&gt;        command1 = self.createElement(action)&lt;br /&gt;        command1.setAttribute("select",selector)&lt;br /&gt;        command1.setAttribute("arg1",css_class)&lt;br /&gt;        command2 = self.createElement(action)&lt;br /&gt;        command2.setAttribute("select",selector)&lt;br /&gt;        command2.setAttribute("value",css_class)&lt;br /&gt;        self.childNodes[0].appendChild(command1)&lt;br /&gt;        self.childNodes[0].appendChild(command2)&lt;br /&gt;    &lt;br /&gt;    def addClass(self,css_class,selector):&lt;br /&gt;        self.CssCommand("addClass",css_class,selector)&lt;br /&gt;&lt;br /&gt;    def removeClass(self,css_class,selector):&lt;br /&gt;        self.CssCommand("remove",css_class,selector)&lt;br /&gt;&lt;br /&gt;    def toggleClass(self,css_class,selector):&lt;br /&gt;        self.CssCommand("toggleClass",css_class,selector)&lt;br /&gt;    &lt;br /&gt;    def append(self,selector,content):&lt;br /&gt;        self.changeContentCommand("append",selector,content)&lt;br /&gt;    &lt;br /&gt;    def prepend(self,selector,content):&lt;br /&gt;        self.changeContentCommand("prepend",selector,content)&lt;br /&gt;        &lt;br /&gt;    &lt;br /&gt;    def before(self,selector,content):&lt;br /&gt;        self.changeContentCommand("before",selector,content)&lt;br /&gt;        &lt;br /&gt;    &lt;br /&gt;    def after(self,selector,content):&lt;br /&gt;        self.changeContentCommand("after",selector,content)&lt;br /&gt;    &lt;br /&gt;    def wrap(self,selector,content):&lt;br /&gt;        self.changeContentCommand("wrap",selector,content)&lt;br /&gt;    &lt;br /&gt;    def replace(self,selector,content):&lt;br /&gt;        self.changeContentCommand("replace",selector,content)&lt;br /&gt;    &lt;br /&gt;    def replaceContent(self,selector,content):&lt;br /&gt;        self.changeContentCommand("replaceContent",selector,content)&lt;br /&gt;    &lt;br /&gt;    def remove(self,selector):&lt;br /&gt;        self.changeStateCommand("remove",selector)&lt;br /&gt;    &lt;br /&gt;    def show(self,selector):&lt;br /&gt;        self.changeStateCommand("show",selector)&lt;br /&gt;    &lt;br /&gt;    def hide(self,selector):&lt;br /&gt;        self.changeStateCommand("hide",selector)&lt;br /&gt;    &lt;br /&gt;    def removeContent(self,selector):&lt;br /&gt;        self.changeStateCommand("empty",selector)&lt;br /&gt;    &lt;br /&gt;    def css(self,selector,property,value):&lt;br /&gt;        command = self.createElement("css")&lt;br /&gt;        command.setAttribute("select",selector)&lt;br /&gt;        command.setAttribute("name",self.camelizeCssProperty(property))&lt;br /&gt;        command.setAttribute("value",value)&lt;br /&gt;        self.childNodes[0].appendChild(command)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 08 May 2008 02:50:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5475</guid>
      <author>desfrenes (desfrenes)</author>
    </item>
    <item>
      <title>Reference of keyCodes </title>
      <link>http://snippets.dzone.com/posts/show/5320</link>
      <description>&lt;code&gt;&lt;br /&gt;    switch (oEvent.keyCode) {&lt;br /&gt;       case 38: //up arrow  &lt;br /&gt;       case 40: //down arrow&lt;br /&gt;       case 37: //left arrow&lt;br /&gt;       case 39: //right arrow&lt;br /&gt;       case 33: //page up  &lt;br /&gt;       case 34: //page down  &lt;br /&gt;       case 36: //home  &lt;br /&gt;       case 35: //end                  &lt;br /&gt;       case 13: //enter  &lt;br /&gt;       case 9: //tab  &lt;br /&gt;       case 27: //esc  &lt;br /&gt;       case 16: //shift  &lt;br /&gt;       case 17: //ctrl  &lt;br /&gt;       case 18: //alt  &lt;br /&gt;       case 20: //caps lock&lt;br /&gt;       case 8: //backspace  &lt;br /&gt;       case 46: //delete&lt;br /&gt;           return true;&lt;br /&gt;           break;&lt;br /&gt;&lt;br /&gt;       default: &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Note: When capturing combination keys there is dedicated boolean attributes for each of the special keys (CTRL, SHIFT, ALT).&lt;br /&gt;Reference: &lt;a href="http://www.sitepoint.com/article/life-autocomplete-textboxes/3"&gt;Make Life Easy With Autocomplete Textboxes [JavaScript &amp; AJAX Tutorials]&lt;/a&gt; [sitepoint.com]</description>
      <pubDate>Wed, 02 Apr 2008 22:31:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5320</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>insertAfter() with insertBefore() and node.nextSibling</title>
      <link>http://snippets.dzone.com/posts/show/5179</link>
      <description>insertAfter() with insertBefore() and node.nextSibling&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Node.prototype.insertAfter = function(newNode, refNode) {&lt;br /&gt;	if(refNode.nextSibling) {&lt;br /&gt;		return this.insertBefore(newNode, refNode.nextSibling);&lt;br /&gt;	} else {&lt;br /&gt;		return this.appendChild(newNode);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.ab-d.fr/"&gt;Source: Benoit Asselin&lt;/a&gt;</description>
      <pubDate>Tue, 26 Feb 2008 20:53:30 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5179</guid>
      <author>ki4ngel (Benoit Asselin)</author>
    </item>
    <item>
      <title>Include CSS Stylesheet by DOM</title>
      <link>http://snippets.dzone.com/posts/show/4554</link>
      <description>&lt;code&gt;&lt;br /&gt;&lt;br /&gt;function includeCSS(p_file) {&lt;br /&gt;	var v_css  = document.createElement('link');&lt;br /&gt;	v_css.rel = 'stylesheet'&lt;br /&gt;	v_css.type = 'text/css';&lt;br /&gt;	v_css.href = p_file;&lt;br /&gt;	document.getElementsByTagName('head')[0].appendChild(v_css);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.ab-d.fr/"&gt;Source: Document Object Model (DOM) Level 1 Specification&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 18 Sep 2007 19:46:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4554</guid>
      <author>ki4ngel (Benoit Asselin)</author>
    </item>
    <item>
      <title>DOM Mouse-Over Element Selection and Isolation</title>
      <link>http://snippets.dzone.com/posts/show/4513</link>
      <description>DOM ISO.v.0.3.0.7.bookmarklet.js&lt;br /&gt;bookmarklet for selecting and isolating an element on a page.&lt;br /&gt;&lt;br /&gt;two sections:&lt;br /&gt;section 1:  Mouseover DOM, setup and handle mouse events and show information about element in informational div.  Click to select, Any key to cancel.&lt;br /&gt;section 2:  Element Isolation with help of XPath.  prompt user for XPath expression  e.g., //DIV[@id='post-body'].  then use XPath to select all elements not(ancestor or descendant or self), then delete those elements.  also ignore self-or-descendants of head and title.&lt;br /&gt;&lt;br /&gt;tools:&lt;br /&gt;Ruderman's javascript development environment:  https://www.squarefree.com/bookmarklets/webdevel.html#jsenv&lt;br /&gt;Mielczarek's js to bookmarklet generator:  http://ted.mielczarek.org/code/mozilla/bookmarklet.html&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;(function() {&lt;br /&gt;	//GLOBALS&lt;br /&gt;		//globals for classMausWork&lt;br /&gt;		var gSelectedElement;	//currently only one selection&lt;br /&gt;		var gHoverElement;		//whatever element the mouse is over&lt;br /&gt;		var gHovering=false;	//mouse is over something&lt;br /&gt;		var gObjArrMW=[];	//global array of classMausWork objects.  for removing event listeners when done selecting.&lt;br /&gt;		&lt;br /&gt;		//extended	&lt;br /&gt;		var infoDiv;		//currently just container for InfoDivHover, might add more here&lt;br /&gt;		var infoDivHover;	//container for hoverText text node.&lt;br /&gt;		var hoverText;		//show information about current element that the mouse is over&lt;br /&gt;		//const EXPERIMENTAL_NEW_CODE=true;	//debugging. new features.&lt;br /&gt;	&lt;br /&gt;	&lt;br /&gt;	//START&lt;br /&gt;	SetupDOMSelection();	&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;		&lt;br /&gt;	//(Section 1) Element Selection&lt;br /&gt;	function SetupDOMSelection()&lt;br /&gt;	{&lt;br /&gt;&lt;br /&gt;		{&lt;br /&gt;			//setup event listeners&lt;br /&gt;			//var pathx="//div | //span | //table | //td | //tr | //ul | //ol | //li | //p";&lt;br /&gt;			var pathx="//div | //span | //table | //th | //td | //tr | //ul | //ol | //li | //p | //iframe";&lt;br /&gt;			var selection=$XPathSelect(pathx);&lt;br /&gt;			for(var element, i=0;element=selection(i);i++)&lt;br /&gt;			{			&lt;br /&gt;				if(element.tagName.match(/^(div|span|table|td|tr|ul|ol|li|p)$/i))	//redundant check.&lt;br /&gt;				{&lt;br /&gt;					var m = new classMausWork(element);&lt;br /&gt;					gObjArrMW.push(m);&lt;br /&gt;					attachMouseEventListeners(m);&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;			document.body.addEventListener('mousedown',MiscEvent,false);&lt;br /&gt;			document.body.addEventListener('mouseover',MiscEvent,false);&lt;br /&gt;			document.body.addEventListener('mouseout',MiscEvent,false);&lt;br /&gt;			document.addEventListener('keypress',MiscEvent,false);&lt;br /&gt;		}&lt;br /&gt;		{&lt;br /&gt;			//setup informational div to show which element the mouse is over.&lt;br /&gt;			infoDiv=document.createElement('div');&lt;br /&gt;			var s=infoDiv.style;&lt;br /&gt;			s.position='fixed';&lt;br /&gt;			s.top='0';&lt;br /&gt;			s.right='0';&lt;br /&gt;			&lt;br /&gt;			s.display='block';&lt;br /&gt;			s.width='auto';&lt;br /&gt;			s.padding='0px';&lt;br /&gt;&lt;br /&gt;			document.body.appendChild(infoDiv);&lt;br /&gt;			infoDivHover=document.createElement('div');&lt;br /&gt;&lt;br /&gt;			s=infoDivHover.style;&lt;br /&gt;			s.fontWeight='bold';			&lt;br /&gt;			s.padding='3px';&lt;br /&gt;			s.Opacity='0.8';&lt;br /&gt;			s.borderWidth='thin';&lt;br /&gt;			s.borderStyle='solid';&lt;br /&gt;			s.borderColor='white';&lt;br /&gt;			s.backgroundColor='black';&lt;br /&gt;			s.color='white';&lt;br /&gt;			&lt;br /&gt;			infoDiv.appendChild(infoDivHover);			&lt;br /&gt;			hoverText=document.createTextNode('selecting');&lt;br /&gt;			infoDivHover.appendChild(hoverText);&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	function CleanupDOMSelection()&lt;br /&gt;	{&lt;br /&gt;		for(var m; m=gObjArrMW.pop(); )&lt;br /&gt;		{&lt;br /&gt;			detachMouseEventListeners(m);&lt;br /&gt;		}&lt;br /&gt;		ElementRemove(infoDiv);&lt;br /&gt;		document.body.removeEventListener('mousedown',MiscEvent,false);&lt;br /&gt;		document.body.removeEventListener('mouseover',MiscEvent,false);&lt;br /&gt;		document.body.removeEventListener('mouseout',MiscEvent,false);		&lt;br /&gt;		document.removeEventListener('keypress',MiscEvent,false);&lt;br /&gt;	}	&lt;br /&gt;&lt;br /&gt;	function attachMouseEventListeners(c)&lt;br /&gt;	{&lt;br /&gt;		//c is object of class classMausWork&lt;br /&gt;		c.element.addEventListener("mouseover",c.mouse_over,false);				&lt;br /&gt;		c.element.addEventListener("mouseout",c.mouse_out,false);	&lt;br /&gt;		c.element.addEventListener("mousedown",c.mouse_click,false);		&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	function detachMouseEventListeners(c)&lt;br /&gt;	{&lt;br /&gt;		//c is object of class classMausWork&lt;br /&gt;		c.resetElementStyle();&lt;br /&gt;		c.element.removeEventListener("mouseover",c.mouse_over,false);				&lt;br /&gt;		c.element.removeEventListener("mouseout",c.mouse_out,false);	&lt;br /&gt;		c.element.removeEventListener("mousedown",c.mouse_click,false);		&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	//mouse event  handling class for element, el.&lt;br /&gt;	function classMausWork(element)&lt;br /&gt;	{	&lt;br /&gt;		//store information about the element this object is assigned to handle. element,  original style, etc.	&lt;br /&gt;		this.element=element;&lt;br /&gt;		&lt;br /&gt;		var elementStyle=element.getAttribute('style');&lt;br /&gt;		var target;&lt;br /&gt;		&lt;br /&gt;		this.mouse_over=function(ev)&lt;br /&gt;		{	&lt;br /&gt;			if(gHovering)return;&lt;br /&gt;			var e=element;			&lt;br /&gt;			var s=e.style;&lt;br /&gt;			s.backgroundColor='yellow';&lt;br /&gt;			s.borderWidth='thin';&lt;br /&gt;			s.borderColor='lime';&lt;br /&gt;			s.borderStyle='solid';					&lt;br /&gt;			InfoMSG(ElementInfo(e),'yellow','blue','yellow');&lt;br /&gt;			gHoverElement=e;&lt;br /&gt;			gHovering=true;&lt;br /&gt;			target=ev.target;&lt;br /&gt;			ev.stopPropagation();		&lt;br /&gt;		};&lt;br /&gt;		&lt;br /&gt;		this.mouse_out=function(ev)&lt;br /&gt;		{&lt;br /&gt;			if(!gHovering)return;&lt;br /&gt;			if(gHoverElement!=element ||ev.target!=target)return;&lt;br /&gt;			var e=element;&lt;br /&gt;			e.setAttribute('style',elementStyle);&lt;br /&gt;			InfoMSG('-','white','black','white');	&lt;br /&gt;			gHoverElement=null;&lt;br /&gt;			gHovering=false;&lt;br /&gt;			target=null;&lt;br /&gt;			//ev.stopPropagation();&lt;br /&gt;		};&lt;br /&gt;		&lt;br /&gt;		this.mouse_click=function(ev)&lt;br /&gt;		{&lt;br /&gt;			if(!gHovering)return;&lt;br /&gt;			if(gHoverElement!=element ||ev.target!=target)return;&lt;br /&gt;			var e=element;&lt;br /&gt;			e.setAttribute('style',elementStyle);&lt;br /&gt;			ev.stopPropagation();			&lt;br /&gt;			CleanupDOMSelection();			&lt;br /&gt;			gHoverElement=null;&lt;br /&gt;			gHovering=false;&lt;br /&gt;			target=null;&lt;br /&gt;			&lt;br /&gt;			if(ev.button==0)&lt;br /&gt;			{&lt;br /&gt;				gSelectedElement=e;&lt;br /&gt;				ElementSelected(e);	//finished selecting, cleanup then move to next part (section 2), element isolation.&lt;br /&gt;			}&lt;br /&gt;		};&lt;br /&gt;		&lt;br /&gt;		this.resetElementStyle=function()&lt;br /&gt;		{&lt;br /&gt;			element.setAttribute('style',elementStyle);&lt;br /&gt;		};		&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	function MiscEvent(ev)		//keypress, and mouseover/mouseout/mousedown event on body.  cancel selecting.&lt;br /&gt;	{&lt;br /&gt;		if(ev.type=='mouseout' &amp;&amp; !gHovering)&lt;br /&gt;		{&lt;br /&gt;			InfoMSG('-','white','black','white');&lt;br /&gt;		}&lt;br /&gt;		else if(ev.type=='mouseover' &amp;&amp; !gHovering)&lt;br /&gt;		{&lt;br /&gt;			InfoMSG('cancel','yellow','red','yellow');&lt;br /&gt;		}&lt;br /&gt;		else //keypress on document or mousedown on body, cancel ops.&lt;br /&gt;		{&lt;br /&gt;			CleanupDOMSelection();&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	function InfoMSG(text,color,bgcolor,border)&lt;br /&gt;	{&lt;br /&gt;		&lt;br /&gt;		var s=infoDivHover.style;&lt;br /&gt;		if(color)s.color=color;&lt;br /&gt;		if(bgcolor)s.backgroundColor=bgcolor;&lt;br /&gt;		if(border)s.borderColor=border;&lt;br /&gt;		if(text)hoverText.data=text;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;	&lt;br /&gt;	&lt;br /&gt;	//(Section 2) Element Isolation&lt;br /&gt;	function ElementSelected(element)	//finished selecting element.  setup string to prompt user.&lt;br /&gt;	{&lt;br /&gt;		PromptUserXpath(ElementInfo(element));&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	&lt;br /&gt;	function PromptUserXpath(defaultpath)		//prompt user, isolate element.&lt;br /&gt;	{&lt;br /&gt;		var userpath = prompt("XPath of elements to isolate : ", defaultpath);&lt;br /&gt;		if(userpath &amp;&amp; userpath.length&gt;0)&lt;br /&gt;		{&lt;br /&gt;			var addPredicate = "[count(./ancestor-or-self::head)=0][count(./ancestor-or-self::title)=0]";	//exclude head &amp; title elements from selection so they aren't removed&lt;br /&gt;			var addPath = "//script | //form | //object | //embed";	//include these elements in selection for removal&lt;br /&gt;			var pathx=TransformXPath_NoAncestorDescendentSelf(userpath, addPredicate, addPath);		//the xpath selection of all elements to be removed/deleted.&lt;br /&gt;			&lt;br /&gt;			try&lt;br /&gt;			{&lt;br /&gt;				var element;&lt;br /&gt;				var elements=$XPathSelect(pathx);	&lt;br /&gt;				for(var i=0;element=elements(i);i++)&lt;br /&gt;				{			&lt;br /&gt;&lt;br /&gt;					if(!element.nodeName.match(/^(head|title)$/i))	//redundant check.&lt;br /&gt;					{&lt;br /&gt;						ElementRemove(element);&lt;br /&gt;					}&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;			&lt;br /&gt;			catch(err)&lt;br /&gt;			{&lt;br /&gt;				alert("wtf: "+err);&lt;br /&gt;			}&lt;br /&gt;			&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;		&lt;br /&gt;	&lt;br /&gt;	//support&lt;br /&gt;	function $XPathSelect(p, context) &lt;br /&gt;	{&lt;br /&gt;	  if (!context) context = document;&lt;br /&gt;	  var i, arr = [], xpr = document.evaluate(p, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);&lt;br /&gt;	  return function(x) { return xpr.snapshotItem(x); };	//closure.  wooot!  returns function-type array of elements (usually elements, or something else depending on the xpath expression).&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	function ElementRemove(e)&lt;br /&gt;	{&lt;br /&gt;		if(e)e.parentNode.removeChild(e);&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	function ElementInfo(element)&lt;br /&gt;	{&lt;br /&gt;		var txt='';&lt;br /&gt;		if(element)&lt;br /&gt;		{&lt;br /&gt;			txt=element.tagName.toLowerCase();		//txt=element.tagName;&lt;br /&gt;			txt=attrib(txt,element,'id');&lt;br /&gt;			txt=attrib(txt,element,'class');	&lt;br /&gt;			txt='//'+txt;&lt;br /&gt;		}&lt;br /&gt;		return txt;&lt;br /&gt;		&lt;br /&gt;		function attrib(t,e,a)&lt;br /&gt;		{			&lt;br /&gt;			if(e.hasAttribute(a))&lt;br /&gt;			{&lt;br /&gt;				t+="[@"+a+"='"+e.getAttribute(a)+"']";&lt;br /&gt;			}&lt;br /&gt;			return t;&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;	&lt;br /&gt;	//function to 'invert' the XPath by selecting all elements that are not ancestor and not descendent and not self.&lt;br /&gt;	function TransformXPath_NoAncestorDescendentSelf(u, includePredicates, includePaths)&lt;br /&gt;	{	&lt;br /&gt;		&lt;br /&gt;		//sample input (u):					//div[@class='sortbox']&lt;br /&gt;		//sample output						//*[  not(./descendant-or-self::*=//div[@class='sortbox'])][  not(./ancestor-or-self::*=//div[@class='sortbox'])]&lt;br /&gt;		//sample output with additional conditions:		//*[  not(./descendant-or-self::*=//div[@class='sortbox'])][  not(./ancestor-or-self::*=//div[@class='sortbox'])][count(./ancestor-or-self::head)=0][count(./ancestor-or-self::title)=0]&lt;br /&gt;		&lt;br /&gt;			//obsolete method.  much faster but can only be used for limited types of (simple) xpath expressions -- unlike the current version, which should be able to convert any xpath.&lt;br /&gt;			//input:			table[@id='topbar']&lt;br /&gt;			//output:			//*[not(./descendant-or-self::table[@id='topbar']) and not(./ancestor-or-self::table[@id='topbar'])]&lt;br /&gt;			//output (alternative):	//*[count(./descendant-or-self::table[@id='topbar'])=0 and count(./ancestor-or-self::table[@id='topbar'])=0]&lt;br /&gt;		&lt;br /&gt;		&lt;br /&gt;		var o1=	'./descendant-or-self::*='+gr(u);&lt;br /&gt;		o1=	'not' + gr(o1);&lt;br /&gt;		o1=	nt(o1);	&lt;br /&gt;		var o2= './ancestor-or-self::*='+gr(u);&lt;br /&gt;		o2=	'not' + gr(o2);&lt;br /&gt;		o2=	nt(o2);&lt;br /&gt;&lt;br /&gt;		var o=	'//*'+o1+o2;&lt;br /&gt;		if(includePredicates &amp;&amp; includePredicates.length&gt;0)	o += includePredicates;&lt;br /&gt;		if(includePaths &amp;&amp; includePaths.length&gt;0) o += ' | ' + includePaths;&lt;br /&gt;		return o;&lt;br /&gt;		&lt;br /&gt;		&lt;br /&gt;		function nt(term){return wrap(term,'[]');}	//node test; predicate - enclose with bracket.&lt;br /&gt;		function gr(term){return wrap(term,'()');}	//group - parenthesize.&lt;br /&gt;		function wrap(term, enclosure){return enclosure.charAt(0)+term+enclosure.charAt(1);}&lt;br /&gt;	}	&lt;br /&gt;	&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;	&lt;br /&gt;})();&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 09 Sep 2007 01:25:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4513</guid>
      <author>jczerg68 (Jon C)</author>
    </item>
    <item>
      <title>HTML/JavaScript - Select list - Add/Remove Options (DOM)</title>
      <link>http://snippets.dzone.com/posts/show/4442</link>
      <description>from http://www.mredkj.com/tutorials/tutorial005.html&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Overview&lt;br /&gt;&lt;br /&gt;    * Insert Before Selected - A new option is created and added above the selected option (as determined by selectedIndex). If none are selected, then no option is added.&lt;br /&gt;    * Remove Selected - Deletes the selected option (or options) from the list. If no options are selected, no options are deleted.&lt;br /&gt;    * Append Last - No matter what is selected, a new option is added at the end.&lt;br /&gt;    * Remove Last - No matter what is selected, the last option is deleted from the list.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Explanation&lt;br /&gt;&lt;br /&gt;According to DOM Level 1, the following is the syntax for the add and remove methods in HTMLSelectElement:&lt;br /&gt;&lt;br /&gt;void add(in HTMLElement element, in HTMLElement before) raises(DOMException);&lt;br /&gt;void remove(in long index);&lt;br /&gt;&lt;br /&gt;The add method takes two arguments: the element to add, and the element to insert before. The spec also says you can add to the end of the list by passing null as the second argument.&lt;br /&gt;&lt;br /&gt;The remove method just takes a number: the index of the option to be removed. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The JavaScript&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;script language="JavaScript" type="text/javascript"&gt;&lt;br /&gt;&lt;!--&lt;br /&gt;var count1 = 0;&lt;br /&gt;var count2 = 0;&lt;br /&gt;&lt;br /&gt;function insertOptionBefore(num)&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  if (elSel.selectedIndex &gt;= 0) {&lt;br /&gt;    var elOptNew = document.createElement('option');&lt;br /&gt;    elOptNew.text = 'Insert' + num;&lt;br /&gt;    elOptNew.value = 'insert' + num;&lt;br /&gt;    var elOptOld = elSel.options[elSel.selectedIndex];  &lt;br /&gt;    try {&lt;br /&gt;      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE&lt;br /&gt;    }&lt;br /&gt;    catch(ex) {&lt;br /&gt;      elSel.add(elOptNew, elSel.selectedIndex); // IE only&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function removeOptionSelected()&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  var i;&lt;br /&gt;  for (i = elSel.length - 1; i&gt;=0; i--) {&lt;br /&gt;    if (elSel.options[i].selected) {&lt;br /&gt;      elSel.remove(i);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function appendOptionLast(num)&lt;br /&gt;{&lt;br /&gt;  var elOptNew = document.createElement('option');&lt;br /&gt;  elOptNew.text = 'Append' + num;&lt;br /&gt;  elOptNew.value = 'append' + num;&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;&lt;br /&gt;  try {&lt;br /&gt;    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE&lt;br /&gt;  }&lt;br /&gt;  catch(ex) {&lt;br /&gt;    elSel.add(elOptNew); // IE only&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function removeOptionLast()&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  if (elSel.length &gt; 0)&lt;br /&gt;  {&lt;br /&gt;    elSel.remove(elSel.length - 1);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;//--&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The HTML&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;form&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="insertOptionBefore(count1++);" /&gt;&lt;br /&gt;Insert Before Selected&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="removeOptionSelected();" /&gt;&lt;br /&gt;Remove Selected&lt;br /&gt;&lt;br /&gt;&lt;select id="selectX" size="10" multiple="multiple"&gt;&lt;br /&gt;&lt;option value="original1" selected="selected"&gt;Orig1&lt;/option&gt;&lt;br /&gt;&lt;option value="original2"&gt;Orig2&lt;/option&gt;&lt;br /&gt;&lt;/select&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="appendOptionLast(count2++);" /&gt;&lt;br /&gt;Append Last&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="removeOptionLast();" /&gt;&lt;br /&gt;Remove Last&lt;br /&gt;&lt;/form&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 22 Aug 2007 12:39:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4442</guid>
      <author>bradalyst (brad)</author>
    </item>
    <item>
      <title>Java DOM: Sample XPath Query</title>
      <link>http://snippets.dzone.com/posts/show/4012</link>
      <description>&lt;code&gt;&lt;br /&gt;    try&lt;br /&gt;    {&lt;br /&gt;      XPath xpath = XPathFactory.newInstance().newXPath();&lt;br /&gt;      Element e = (Element) xpath.evaluate("/Archive/Section/Description", parentNode,&lt;br /&gt;          XPathConstants.NODE);&lt;br /&gt;      printXmlNode(e);&lt;br /&gt;    }&lt;br /&gt;    catch (XPathExpressionException e)&lt;br /&gt;    {&lt;br /&gt;      e.printStackTrace();&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 14 May 2007 09:22:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4012</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
    <item>
      <title>Java DOM: Printing the Content of a Node</title>
      <link>http://snippets.dzone.com/posts/show/4011</link>
      <description>&lt;code&gt;&lt;br /&gt;    try&lt;br /&gt;    {&lt;br /&gt;      // Set up the output transformer&lt;br /&gt;      TransformerFactory transfac = TransformerFactory.newInstance();&lt;br /&gt;      Transformer trans = transfac.newTransformer();&lt;br /&gt;      trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");&lt;br /&gt;      trans.setOutputProperty(OutputKeys.INDENT, "yes");&lt;br /&gt;&lt;br /&gt;      // Print the DOM node&lt;br /&gt;&lt;br /&gt;      StringWriter sw = new StringWriter();&lt;br /&gt;      StreamResult result = new StreamResult(sw);&lt;br /&gt;      DOMSource source = new DOMSource(node);&lt;br /&gt;      trans.transform(source, result);&lt;br /&gt;      String xmlString = sw.toString();&lt;br /&gt;&lt;br /&gt;      System.out.println(xmlString);&lt;br /&gt;    }&lt;br /&gt;    catch (TransformerException e)&lt;br /&gt;    {&lt;br /&gt;      e.printStackTrace();&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 14 May 2007 09:19:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4011</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
    <item>
      <title>Java DOM : Creating an XML Document from XML File</title>
      <link>http://snippets.dzone.com/posts/show/4010</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    try&lt;br /&gt;    {&lt;br /&gt;      //&lt;br /&gt;      // Create the XML Document&lt;br /&gt;      //&lt;br /&gt;&lt;br /&gt;      DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();&lt;br /&gt;      DocumentBuilder docBuilder = dbfac.newDocumentBuilder();&lt;br /&gt;      Document doc = docBuilder.parse(filePath);&lt;br /&gt;&lt;br /&gt;      // ...&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;    catch (Exception e)&lt;br /&gt;    {&lt;br /&gt;      // ...&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 14 May 2007 08:52:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4010</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
    <item>
      <title>Simple Java DOM XML Processing Example</title>
      <link>http://snippets.dzone.com/posts/show/4009</link>
      <description>// Simple Java DOM XML Processing Example&lt;br /&gt;// Source: http://www.genedavis.com/library/xml/java_dom_xml_creation.php&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;import java.io.*;&lt;br /&gt;&lt;br /&gt;import org.w3c.dom.*;&lt;br /&gt;&lt;br /&gt;import javax.xml.parsers.*;&lt;br /&gt;&lt;br /&gt;import javax.xml.transform.*;&lt;br /&gt;import javax.xml.transform.dom.*;&lt;br /&gt;import javax.xml.transform.stream.*;&lt;br /&gt;&lt;br /&gt;public class DomXmlExample {&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * Our goal is to create a DOM XML tree and then print the XML.&lt;br /&gt;     */&lt;br /&gt;    public static void main (String args[]) {&lt;br /&gt;        new DomXmlExample();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public DomXmlExample() {&lt;br /&gt;        try {&lt;br /&gt;            /////////////////////////////&lt;br /&gt;            // Creating an empty XML Document&lt;br /&gt;&lt;br /&gt;            // We need a Document&lt;br /&gt;            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();&lt;br /&gt;            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();&lt;br /&gt;            Document doc = docBuilder.newDocument();&lt;br /&gt;&lt;br /&gt;            ////////////////////////&lt;br /&gt;            // Creating the XML tree&lt;br /&gt;&lt;br /&gt;            // create the root element and add it to the document&lt;br /&gt;            Element root = doc.createElement("root");&lt;br /&gt;            doc.appendChild(root);&lt;br /&gt;&lt;br /&gt;            // create a comment and put it in the root element&lt;br /&gt;            Comment comment = doc.createComment("Just a thought");&lt;br /&gt;            root.appendChild(comment);&lt;br /&gt;&lt;br /&gt;            // create child element, add an attribute, and add to root&lt;br /&gt;            Element child = doc.createElement("child");&lt;br /&gt;            child.setAttribute("name", "value");&lt;br /&gt;            root.appendChild(child);&lt;br /&gt;&lt;br /&gt;            // add a text element to the child&lt;br /&gt;            Text text = doc.createTextNode("Filler, ... I could have had a foo!");&lt;br /&gt;            child.appendChild(text);&lt;br /&gt;&lt;br /&gt;            /////////////////&lt;br /&gt;            // Output the XML&lt;br /&gt;&lt;br /&gt;            // set up a transformer&lt;br /&gt;            TransformerFactory transfac = TransformerFactory.newInstance();&lt;br /&gt;            Transformer trans = transfac.newTransformer();&lt;br /&gt;            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");&lt;br /&gt;            trans.setOutputProperty(OutputKeys.INDENT, "yes");&lt;br /&gt;&lt;br /&gt;            // create string from xml tree&lt;br /&gt;            StringWriter sw = new StringWriter();&lt;br /&gt;            StreamResult result = new StreamResult(sw);&lt;br /&gt;            DOMSource source = new DOMSource(doc);&lt;br /&gt;            trans.transform(source, result);&lt;br /&gt;            String xmlString = sw.toString();&lt;br /&gt;&lt;br /&gt;            // print xml&lt;br /&gt;            System.out.println("Here's the xml:\n\n" + xmlString);&lt;br /&gt;&lt;br /&gt;        } catch (Exception e) {&lt;br /&gt;            System.out.println(e);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 14 May 2007 08:04:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4009</guid>
      <author>thitiv (Thiti V. Sintopchai)</author>
    </item>
  </channel>
</rss>
