[thelist] js xml2Object

Christian Giordano ml at nuthinking.com
Tue Nov 1 05:06:25 CST 2005


well, making many tests I made a spaghetti one by myself:

function xmlToObject(oXML){
	var oRes=new Object();
	translateXML(oXML,oRes);
	return oRes;
}
function translateXML (oNode,pNode){
	if(oNode.hasChildNodes){
		var e=oNode.firstChild;
		while(e!=null){
			if(!pNode[e.nodeName]) pNode[e.nodeName]=new Array();
			var o=new Object();
			if(e.hasChildNodes && !e.firstChild.hasChildNodes && 
e.firstChild.nodeValue!=null){
				translateXML(e.firstChild,o);
			}else{
				translateXML(e,o);
			}
			pNode[e.nodeName].push(o);
			e=e.nextSibling;
		}
		pNode.attributes=xmlAttributes(oNode);
	}else{
		if(oNode.nodeValue==null && oNode.attributes){
			pNode.attributes=xmlAttributes(oNode);
		}else{
			if(oNode.parentNode) pNode.attributes=xmlAttributes(oNode.parentNode);
		}
		pNode.data=oNode.nodeValue;
	}
}

It seems to work properly, if someone has some suggestions, they are 
more then welcome!


chr


-- 
___________________________________________________________________
{ Christian Giordano's site and blog @ http://cgws.nuthinking.com }




More information about the thelist mailing list