I've used this code with xml returned from ajax ... it may work for Jon.

John, how would you make this code better?

$.fn.xml = function () {return $.xml(this[0])}
$.xml = function(xml) { // dump the xml back to html text
   if (!xml) return ""
   var res = ""
   var tag = xml.nodeName
   var showTheTag = tag.charAt(0) != "#"
   if (showTheTag) res += '<' + tag
   if (xml.hasAttributes()) {
       var attrs = xml.attributes
       for (var i = 0; i < attrs.length; i++){
           var attr=attrs[i]
           if (attr.specified) res +=' ' + attr.name + '="' + attr.value +
'"'
       }
   }
   if (showTheTag) res+= ">"
   if (xml.nodeType == 8){
       res += "<!-- " + xml.nodeValue + " -->"
   } else if (xml.nodeValue != null){
       res +=  xml.nodeValue.replace(/\&/g,"&amp;").replace(/\</g,"&lt;")
   }
   if (xml.hasChildNodes()) {
       var children = xml.childNodes
       for (var i = 0; i < children.length; i++){
           var child = children[i]
           res += $.xml(child)
       }
   }
   if (showTheTag)  res += '</' + tag + '>'
   return res
}



On 4/27/07, John Resig <[EMAIL PROTECTED]> wrote:


If you wanted to do it right, you'd want to do something like this:

jQuery.fn.childNodes = function() {
     return this.pushStack( jQuery.map( this,
"jQuery.makeArray(a.childNodes)" ) );
};

HUGE WARNING! (this is why we don't include this method in jQuery
right now) jQuery does not expect itself to handle non-elements. For
example, if were to do:

$("body").childNodes().css("color", "blue");

It would die with an exception, because you can't set a color on a text
node.

In fact, there's already a bug open for this specific feature requeset:
http://dev.jquery.com/ticket/1024

--John

On 4/27/07, Jonathan Sharp <[EMAIL PROTECTED]> wrote:
> So we have a situation where we need to clone all of an elements
childNodes
> (including whitespace & text nodes). The code below is an attempt to add
a
> childNodes() function that behaves similar to children() except for it
> includes the childNodes.
>
> Comments/houghts/suggestions/feedback?
>
> $.fn.childNodes = function() {
>     return this.pushStack( this.get(0).childNodes || [] );
> };
>
> Cheers,
> -js




--
Ⓙⓐⓚⓔ - יעקב   ʝǡǩȩ   ᎫᎪᏦᎬ

Reply via email to