martinlawrence Posted July 25, 2007 Share Posted July 25, 2007 I'm building a function to iterate over a xml dom tree and create a html dom with the same structure, to avoid the use of innerHTML but to do that independently of the xml received and previously knowing that it will be used as html. the function iterates well over the xml tree but fails to create the html object as i want to, and produces strange results as they are different each time i try the function (that's the first time i see something like this happen, and it happens even in Firefox and Opera). Will appreciate some help. (lines "* != ftp://ftp" are "*!=U.NDEFINED" but it gets replaced here when posted function convertObjectXMLtoHTML(xmlObject) { XMLnodeHasSon = function(XMLnode){ return ( XMLnode.childNodes[0] != undefined ) ;} XMLnodeHasSibling = function(XMLnode){ return ( XMLnode.nextSibling != undefined ) ;} XMLnodeHasParent = function(XMLnode){ return ( XMLnode.parentNode != undefined ) ;} baseXMLnode = xmlObject.childNodes[0]; if (baseXMLnode.nodeName == "xml") /* Internet Explorer 6 first element is the xml tag*/ baseXMLnode = baseXMLnode.nextSibling; XMLnode = baseXMLnode; HTMLnode = document.createElement(baseXMLnode.nodeName ); while(XMLnode) { if (XMLnodeHasSon(XMLnode)) { XMLnode = XMLnode.childNodes[0]; /* XMLnode will "point" to his firstChild */ /* Type 1 is tags, type 3 is inner text*/ if (XMLnode.nodeType == 1 ) HTMLnodeChild = document.createElement(XMLnode.nodeName); else if (XMLnode.nodeType == 3 ) HTMLnodeChild = document.createTextNode(XMLnode.nodeValue); HTMLnode.appendChild(HTMLnodeChild); /* Create a Child in the HTML object equal to the one of XML */ HTMLnode = HTMLnode.firstChild; /* HTMLnode will "point" to his firstChild */ } else /* If the node doesn't have childs (after iteration) , we have to find the upper node in the tree that has a sibling, create it in HTML and search again for childs */ { while (!XMLnodeHasSibling(XMLnode)) { if (XMLnodeHasParent(XMLnode)) { /* Up in the tree */ XMLnode = XMLnode.parentNode; HTMLnode = HTMLnode.parentNode; } else { /* Reaching the primary element, iteration will end */ XMLnode = null; break; } } if(XMLnode) { XMLnode = XMLnode.nextSibling; /* As this node as a sibling XMLnode will point to it */ HTMLnodeSibling = document.createElement(XMLnode.nodeName ); /* copy of the sibling */ HTMLnode.parentNode.appendChild(HTMLnodeSibling); /* appending the sibling to the parent, the other son of the parent is a sibling */ HTMLnode = HTMLnode.parentNode.lastChild; /* "pointing" HTMLnode to the sibling appended */ } } } return HTMLnode; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.