Re: [jQuery] Serializing XML

2006-10-04 Thread John Resig
> There is Object.toXML(), can be found here:
> http://jquery.com/dev/svn/jquery/build/js/xml.js Not really something
> special by jQuery, but should do the job.

Well, it is sort of specific to jQuery, because I wrote it ;-) With
that being said, I wouldn't trust that code any more than I could
throw it. It works for us, using the server-side Rhino code, doesn't
generate any errors - but I wouldn't recommend using it in any other
case (at least not yet).

If you're just doing browser-based XML generation, then the
afformentioned XMLSerialize technique should fit nicely. My only
recommendation is to name it .toXML() instead of just .xml().

--John

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Serializing XML

2006-10-03 Thread Ⓙⓐⓚⓔ
It's nice that we  agree on so many levels! ! It's even good when we
don't! at least we learn about other opinions!


-- 
Ⓙⓐⓚⓔ - יעקב   ʝǡǩȩ   ᎫᎪᏦᎬ
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Serializing XML

2006-10-03 Thread Christof Donat
Hi,

> I wasn't claiming that for was any less great than each! I was
> complaining about looping a concat vs. doing a join at t the end...

Erm, did I discuss that somewhere? You answered to my posting. The concat vs. 
join stuff was Sam.

My point was to take the check for XMLSerializer out of the function by 
emulating it when it is not yet implemented. That is not only a little bit 
faster. It is a more general solution which is much more important than the 
rather marginal speedup.

> and other micro effeciencies. I gave up tiny-tuning code when I gave
> up assembler.

There are situations where you need speed optimizations. Especially with 
languages like JavaScript, where some interpreters are really slow, it 
happens as soon as you work with big datasets. I like to spot eventual 
performance improvements to keep me trained for the cases I need to.

> I tell this to all my programmers, You can't compare a little
> application code time to the time it takes to do a big IO (or http or
> sql...).

It all depends on how often you run the loop and how much overhead 
the "optimization" needs. If the loop costs maybe 10 bytes of JS-code and is 
run some 1 times the optimization will probably pay.

Of course a "optimization" in one function that slows down other stuff so much 
that the complete system gets slower is no real optimization.

> I tend to be opinionated about certain things... I am an XSL guy, I
> don't like incrementing! (a for loop works when there is little else!)

As I said: "With such a simple function in each() you might also consider to 
use a normal for-loop." Note "simple function" and "might consider".

I think we actually do agree here: for() is OK in such simple cases if 
performance matters. In more complicated cases each() makes the code better 
readable. I also think that Sams optimization is OK, if performance matters 
(i.e. the function is called very often with huge datasets).

Christof

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Serializing XML

2006-10-03 Thread Ⓙⓐⓚⓔ
I wasn't claiming that for was any less great than each! I was
complaining about looping a concat vs. doing a join at t the end...
and other micro effeciencies. I gave up tiny-tuning code when I gave
up assembler.

I tell this to all my programmers, You can't compare a little
application code time to the time it takes to do a big IO (or http or
sql...).

I tend to be opinionated about certain things... I am an XSL guy, I
don't like incrementing! (a for loop works when there is little else!)

On 10/3/06, Christof Donat <[EMAIL PROTECTED]> wrote:
> Hi,
>
> > I like optimized code as much as the next guy... but brevity and
> > readability is KEY. the milliseconds that you can save using one
> > reasonable technique vs. another are not comparable  to the seconds it
> > takes for an http request.
>
> Is this about my suggestion to use for() instead of each()? If not, my code is
> not noticably longer than Marks. I also don't think that it is less readable.
>
> If you are just talking about the for()-each() stuff. Both have their
> advantages and their disadvantages. If it is the inner loop of a function you
> expect to call often, you may whant to pay the price of less readability and
> brevity for the performance of for().
>
> > I never liked using XMLSerializer because [...]
>
> It can be quite usefull with XMLHttpRequests when your protocol is XML-based.
> But that was not the question here, was it?
>
> Christof
>
> ___
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
>


-- 
Ⓙⓐⓚⓔ - יעקב   ʝǡǩȩ   ᎫᎪᏦᎬ
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Serializing XML

2006-10-03 Thread Christof Donat
Hi,

> I like optimized code as much as the next guy... but brevity and
> readability is KEY. the milliseconds that you can save using one
> reasonable technique vs. another are not comparable  to the seconds it
> takes for an http request.

Is this about my suggestion to use for() instead of each()? If not, my code is 
not noticably longer than Marks. I also don't think that it is less readable.

If you are just talking about the for()-each() stuff. Both have their 
advantages and their disadvantages. If it is the inner loop of a function you 
expect to call often, you may whant to pay the price of less readability and 
brevity for the performance of for().

> I never liked using XMLSerializer because [...]

It can be quite usefull with XMLHttpRequests when your protocol is XML-based. 
But that was not the question here, was it?

Christof

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Serializing XML

2006-10-02 Thread Ⓙⓐⓚⓔ
I like optimized code as much as the next guy... but brevity and
readability is KEY. the milliseconds that you can save using one
reasonable technique vs. another are not comparable  to the seconds it
takes for an http request.

I never liked using XMLSerializer because I can't hit the tags &
attributes as I serialize. Of course if you just want to copy the dom
into a string, it does the job for you pretty well. And the addition
of .xml seems like it will even keep IE happy. I guess we can expect
the next cool browser to implement XMLSerializer, but a final else
might be nice to handle stupid versions of js. Or at least an
alert("stupid browser does not support XMLSerializer").

Jake

On 10/2/06, Christof Donat <[EMAIL PROTECTED]> wrote:
> Hi,
>
> > $.fn.serializeXML = function () {
> >   var out = '';
> >   if (typeof XMLSerializer == 'function') {
> >   var xs = new XMLSerializer();
> >   this.each(function() {
> >   out += xs.serializeToString(this);
> >   });
> >   } else if (this[0] && this[0].xml != 'undefined') {
> >   this.each(function() {
> >   out += this.xml;
> >   });
> >   }
> >   return out;
> > };
>
> Maybe this could make the code simpler:
>
> if( typeof XMLSerializer != 'function' ) {
> XMLSerializer = function() {};
> XMLSerializer.prototype.serializeToString = function(elm) {
> return eml.xml;
> }
> }
>
> Then you just need
>
> $.fn.xml = function () {
> var out = '';
> var xs = new XMLSerializer();
> this.each(function() {
> out += xs.serializeToString(this);
> });
> return out;
> };
>
> Advantages:
> 1. Your code checks for XMLSerializer with every call. Mine only once.
> 2. You can add another check to see if there is a .xml-Property. If that is
> not available you can have your own (slower) serializer-function that simply
> traverses the DOM.
> 3. By resembling the XMLSerializer-API (not complete now) you can make third
> party code work that needs XMLSerializer.
>
> BTW.: With such a simple function in each() you might also consider to use a
> normal for-loop:
> for( var i = 0; i < this.length; i++ ) out += xs.serializeToString(this[i]);
>
> That doesn't look so fancy, but it should be faster.
>
> Christof
>
> ___
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
>


-- 
Ⓙⓐⓚⓔ - יעקב   ʝǡǩȩ   ᎫᎪᏦᎬ
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Serializing XML

2006-10-02 Thread Christof Donat
Hi,

> $.fn.serializeXML = function () {
>   var out = '';
>   if (typeof XMLSerializer == 'function') {
>   var xs = new XMLSerializer();
>   this.each(function() {
>   out += xs.serializeToString(this);
>   });
>   } else if (this[0] && this[0].xml != 'undefined') {
>   this.each(function() {
>   out += this.xml;
>   });
>   }
>   return out;
> };

Maybe this could make the code simpler:

if( typeof XMLSerializer != 'function' ) {
XMLSerializer = function() {};
XMLSerializer.prototype.serializeToString = function(elm) {
return eml.xml;
}
}

Then you just need

$.fn.xml = function () {
var out = '';
var xs = new XMLSerializer();
this.each(function() {
out += xs.serializeToString(this);
});
return out;
};

Advantages:
1. Your code checks for XMLSerializer with every call. Mine only once.
2. You can add another check to see if there is a .xml-Property. If that is 
not available you can have your own (slower) serializer-function that simply 
traverses the DOM.
3. By resembling the XMLSerializer-API (not complete now) you can make third 
party code work that needs XMLSerializer.

BTW.: With such a simple function in each() you might also consider to use a 
normal for-loop:
for( var i = 0; i < this.length; i++ ) out += xs.serializeToString(this[i]);

That doesn't look so fancy, but it should be faster.

Christof

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Serializing XML

2006-10-02 Thread Sam Collett
On 02/10/06, Mark Gibson <[EMAIL PROTECTED]> wrote:
> Ok, after some searching around the web, I believe that
> Firefox, Opera, and Safari all implement XMLSerializer,
> whilst IE has an 'xml' property on every node.
> So here's my XML serializer plugin:
>
> $.fn.serializeXML = function () {
> var out = '';
> if (typeof XMLSerializer == 'function') {
> var xs = new XMLSerializer();
> this.each(function() {
> out += xs.serializeToString(this);
> });
> } else if (this[0] && this[0].xml != 'undefined') {
> this.each(function() {
> out += this.xml;
> });
> }
> return out;
> };
>
> It seems to work in FF 1.5.0.7 and IE 6, but needs testing
> in Opera and Safari.
>
> - Mark Gibson

Perhaps it would be better (performance wise) to use an array?

var out = new Array();
...
out.push(xs.serializeToString(this));
...
return out.join("");

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Serializing XML

2006-10-02 Thread Brandon Aaron
I can test in Safari and Opera if you'll provide an example/test page. :)

--
Brandon Aaron

On 10/2/06, Mark Gibson <[EMAIL PROTECTED]> wrote:
> Ok, after some searching around the web, I believe that
> Firefox, Opera, and Safari all implement XMLSerializer,
> whilst IE has an 'xml' property on every node.
> So here's my XML serializer plugin:
>
> $.fn.serializeXML = function () {
> var out = '';
> if (typeof XMLSerializer == 'function') {
> var xs = new XMLSerializer();
> this.each(function() {
> out += xs.serializeToString(this);
> });
> } else if (this[0] && this[0].xml != 'undefined') {
> this.each(function() {
> out += this.xml;
> });
> }
> return out;
> };
>
> It seems to work in FF 1.5.0.7 and IE 6, but needs testing
> in Opera and Safari.
>
> - Mark Gibson
>
> ___
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
>

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Serializing XML

2006-10-02 Thread Mark Gibson
Ok, after some searching around the web, I believe that
Firefox, Opera, and Safari all implement XMLSerializer,
whilst IE has an 'xml' property on every node.
So here's my XML serializer plugin:

$.fn.serializeXML = function () {
var out = '';
if (typeof XMLSerializer == 'function') {
var xs = new XMLSerializer();
this.each(function() {
out += xs.serializeToString(this);
});
} else if (this[0] && this[0].xml != 'undefined') {
this.each(function() {
out += this.xml;
});
}
return out;
};

It seems to work in FF 1.5.0.7 and IE 6, but needs testing
in Opera and Safari.

- Mark Gibson

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Serializing XML

2006-10-02 Thread Mark Gibson
Sam Collett wrote:
> On 02/10/06, Mark Gibson <[EMAIL PROTECTED]> wrote:
>> Hello,
>> I've search high and low, but can't find a method of serializing XML
>> with jQuery. Have I missed something, or should I start writing a
>> new plugin? ($.fn.serializeXML)
>>
>> I know that firefox has XMLSerializer(), any ideas for IE, Safari,
>> Opera? Maybe just a hand coded JS serializing routine?
> 
> I don't think there is any way of serializing XML in jQuery without
> resorting to a plugin (I don't know of any plugins that can do this).
> 
> I think a multi-purpose serializer would be good:
> JS Object <--> XML <--> JSON <--> JS Object

I was specifically thinking of just serializing DOM objects to strings,
anything beyond that requires some kind of mapping.

Here's a simple implementation using the XMLSerialize object:

$.fn.serializeXML = function () {
var out = '';
if (typeof XMLSerializer == 'function') {
var xs = new XMLSerializer();
this.each(function() {
out += xs.serializeToString(this);
});
} else {
// TODO: Find another serializer, or manually serialize
}
return out;
};

This will need to be fleshed out for other browsers.

Does anyone know of native serialization methods in IE, Safari, Opera?
or do any of these support XMLSerialize()?

- Mark Gibson

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Serializing XML

2006-10-02 Thread Jörn Zaefferer
Mark Gibson schrieb:
> Hello,
> I've search high and low, but can't find a method of serializing XML
> with jQuery. Have I missed something, or should I start writing a
> new plugin? ($.fn.serializeXML)
>
> I know that firefox has XMLSerializer(), any ideas for IE, Safari,
> Opera? Maybe just a hand coded JS serializing routine?
>   
There is Object.toXML(), can be found here: 
http://jquery.com/dev/svn/jquery/build/js/xml.js Not really something 
special by jQuery, but should do the job.

-- Jörn

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Serializing XML

2006-10-02 Thread Sam Collett
On 02/10/06, Mark Gibson <[EMAIL PROTECTED]> wrote:
> Hello,
> I've search high and low, but can't find a method of serializing XML
> with jQuery. Have I missed something, or should I start writing a
> new plugin? ($.fn.serializeXML)
>
> I know that firefox has XMLSerializer(), any ideas for IE, Safari,
> Opera? Maybe just a hand coded JS serializing routine?
>
> - Mark Gibson

I don't think there is any way of serializing XML in jQuery without
resorting to a plugin (I don't know of any plugins that can do this).

I think a multi-purpose serializer would be good:
JS Object <--> XML <--> JSON <--> JS Object

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


[jQuery] Serializing XML

2006-10-02 Thread Mark Gibson
Hello,
I've search high and low, but can't find a method of serializing XML
with jQuery. Have I missed something, or should I start writing a
new plugin? ($.fn.serializeXML)

I know that firefox has XMLSerializer(), any ideas for IE, Safari,
Opera? Maybe just a hand coded JS serializing routine?

- Mark Gibson

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/