Friday, April 30, 2010

Difference between SAX and DOM parser ? Which parser to Use ?

SAX (Simple API for XML)
DOM (Document Object Model)

XML is type of data base also it is data accessing objects for communication between different products written in different languages.

why we need sax parser in first place while we have traditional DOM parser at hand ?

DOM

DOM parser reads XML file and create tree based data models (objects corresponding to each tag ) in memory and thus loads whole tree at the time of reading the XML file .As whole file is loaded in memory we can get objects like all children's of node , parent of node etc .Means it can traverse from root to inner most element also from innermost element to root or any element .It can insert / update any element due to this structure .DOM parser is also used by browsers to show XSLT parsing as HTML. This is easy way to manipulate the XML files .Now is we have very large XML file and if we try to parse it with DOM from java then "JVM out of memory " exception will be thrown as it hogs all heap space .Then intuitively we can say that to avoid the problem we should load nodes which are only needed at a time in the memory ... which leads to SAX parser creation .

SAX parser

It uses event bases navigation to get particular element of XML at a time .It uses certain event based handlers to look required node .They are like start element , end of element , error etc.Because of this nature it can parse XML of gigabytes in size as it does not create DOM structure of object models in memory .Because of this it also fast and need relatively less memory .APIs of SAX parser are quite complex than intuitive DOM parser .It uses streamed reading of XML file than entire file in one time .

Java SAX parser have problem in multi threaded environment so the out put may be corrupt then two threads are accessing the document at same time .So it is needed to create different instance of SAXParser for each new thread.

It has problem when it needed whole element tree in DTD validation .

Choice -

For reading the large XML document SAX parser is best choice and it is faster than DOM in term of speed . If you want to manipulate the tree elements of large XML then it is very difficult
with this parser because you have to write down your own custom implementation .

DOM parser is made upon W3-DOM specifications and it is very easy in traversing and manipulating /inserting new elemets in XML .


Speed of DOM , SAX , XSLT for XML parsing


Wednesday, April 21, 2010

Cutting edge table rows reorder control

By Neelesh Salpe


Problem with firefox is that it does not support DOM method
table is element .
table.moveRow(fromIndex, toIndex);


There is work arround for this

JS mthods after clicking on " Move Up method "
You can infer method for "Move Down " from above example.



function moveUpRow(tableId) {
var fromindex = null;

var trs = document.getElementById(tableId)
.getElementsByTagName('tbody')[0].getElementsByTagName('tr');


for ( var i = 0; i < classname ="=" fromindex =" i;"> 0){

var table = document.getElementById(tableId);

moveRow(table,(parseInt(fromindex) - 1),parseInt(fromindex));
moveUp(fromindex);
}

}







function moveRow(table, from, to)
{
var tbody = table.tBodies[0]; // Use tbody
var trFrom = tbody.rows[from]; // Make sure row stays referenced
tbody.removeChild(trFrom); // Remove the row before inserting it (dupliate id's etc.)
var trTo = tbody.rows[to];
tbody.insertBefore(trFrom, trTo);
}



Above code is working and tested for IE8, firewfox3.5 , safari , opera