Jump to content

Converting XML DOM object to HTML DOM object


martinlawrence

Recommended Posts

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;   

 

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.