I had the same problem, where I have to rely on an XHTML+XML output already
processed through XSLT, from which I need to extract parts of and insert
them into the DOM.

I've taken your code and changed it a little to get it more universal.

if (!document.ELEMENT_NODE) {
        document.ELEMENT_NODE = 1;
        document.ATTRIBUTE_NODE = 2;
        document.TEXT_NODE = 3;
        document.CDATA_SECTION_NODE = 4;
        document.ENTITY_REFERENCE_NODE = 5;
        document.ENTITY_NODE = 6;
        document.PROCESSING_INSTRUCTION_NODE = 7;
        document.COMMENT_NODE = 8;
        document.DOCUMENT_NODE = 9;
        document.DOCUMENT_TYPE_NODE = 10;
        document.DOCUMENT_FRAGMENT_NODE = 11;
        document.NOTATION_NODE = 12;
}


/* translate the current node to a string, keeping all attributes, text
nodes and attribute names. */
function domToString(node) {
    if (!node) return "";
    var desc = "";
    //Text nodes do not have child elements,
    //so return immediately after processing.
    if (node.nodeType == document.TEXT_NODE) {
        if (typeof (node.nodeValue) != 'undefined') desc += node.nodeValue;
    }
    else {
        //node name.
        desc += "<" + node.nodeName;
        for (i = 0; i < node.attributes.length; i++) {
            if (node.attributes[i].specified) {
                desc += " " + node.attributes[i].nodeName + "=\"" +
node.attributes[i].nodeValue + "\"";
            }
        }
        /*
        If node has no children, then use compact notation
        This is important for elements like <br/>, where some browsers 
        interpret the opening tag of <br></br> as <br/>.
        On the other hand, elements like script and style must have a
closing tag.
        */
        
        if (node.hasChildNodes() || node.nodeName == 'script' ||
node.nodeName == 'style') {
            desc += ">";
            $(node).contents().each(function() {
                desc += domToString(this);
            });
            desc += "</" + node.nodeName + ">";
        }
        else {
            desc += "/>";
        }
    }

    return desc;
}

Usage: domToString(node), which returns a string.







dbu wrote:
> 
> 
> thanks NightWatchman for the input. it might work, but i really did not
> feel like changing the complete xml generating code...
> 
> i'm surprised nobody else has run into this problem - or cared to share
> his insights on any forum or blog post or whatever (i googled a lot
> before posting here...).
> 
> anyways, what worked for me:
> 
> 
> var desc="";
> 
> /* translate the current node to a string, keeping all attributes, text
> nodes and attribute names. */
> function domToString() {
>     if (this.nodeName == "#text") {
>         if (this.nodeValue != 'undefined') desc += this.nodeValue;
>         return;
>     }
>     desc += "<" + this.nodeName;
>     for (i=0; i<this.attributes.length;i++) {
>         if (this.attributes[i].specified) {
>             desc += " " +
> this.attributes[i].nodeName+"='"+this.attributes[i].nodeValue+"'";
>         }
>     }
> 
>     desc += ">";
>     $(this).contents().each(domToString);
>     desc += "</" + this.nodeName + ">";
> }
> 
> desc=""; //reset desc if you use it more than once.
> $("text",node).children().each(domToString);
> 
> 
> then i get the html string in desc, and can use that with .html() or
> .append() or whatever. or in my case the google maps api, which also
> accepts html code as string.
> 
> i know, the global variable is not really nice, but i did not find a
> better solution right on the spot and this works for me...
> 
> 
> 

-- 
View this message in context: 
http://old.nabble.com/%24.get-xml-document-with-xhtml-fragments-in-IE-tp22378044s27240p26421378.html
Sent from the jQuery General Discussion mailing list archive at Nabble.com.

Reply via email to