View Full Version : Streaming XML with AJAX

sir pannels
12-06-2006, 03:59 PM
Hi there,

Is it possible to stream XML data from a file using AJAX?
The xml file is constantly being updated by a backend program, I currently have a javascript that has a timer, every second it just runs a loadXML() function to return the XML data however having a few problems with the javascript tripping over itself.

Wondering if AJAX has streaming abilities?

Also, can someone please clarify for me, is the xml dom javascript, or ajax? As I have just read a ajax book that tells you to use xmldom to read xml data but im already doing that, with javascript, what is the difference?

Cheers for your time,
Sir P :D!

sir pannels
12-06-2006, 04:37 PM

thanks for your reply and time :)

Yes sorry by read I mean parse.

The file is indeed being writen more often than every second, as soon as 1 milisecond .. gulp ..
Also, I'm unable to create another file on the device with the time stamp due to storage space destrictions.

Cheers, any further help is apperciated
cheers again
Sir P

12-06-2006, 07:40 PM
You might want to have a look at an AJAX server push technology such as COMET (http://en.wikipedia.org/wiki/Comet_%28programming%29). This enables streaming, but requires a bit of setup beforehand. I've used it for streaming financial market data without using a plugin - only using javascript.

So in answer to your question of "is it possible to stream data" the answer is yes - your server push chooses which data to send. In answer to the query about the DOM:

The DOM (document object model) is the tree structure of an html/xml document. This is entirely seperate from javascript which is the scripting language for the browser. It's a little more complicated than that, but that's pretty much the greatest hits. So javascript and the DOM are seperate entities - but javascript can send and retrieve information to and from the DOM. AJAX - or rather XMLHTTP to call it a more specific name for this discussion - is a data exchange mechanism to move information between the server and the client (the browser). This data exchange can use the XML DOM as a standard format to pass the data between the server and the client (the responseXML attribute of the javascript XMLHTTP object). It can also pass strings between the server and client (the responseText attribute of the XMLHTTP object). So you can see that javascript and the DOM, although seperate, can and do interact at certain times.

hope this helps


12-06-2006, 10:45 PM
XML can be streamed. I put together an example. You click the Start button to start it streaming in the xml with a 5 second pause in between loads. The other button will create the XML file with the reverse of what you type in. You can see how if you type in something it will print out the reverse. The streaming part is hard to see since the file is small but you can look at the code. But you could imagine if you were loading a bunch of little XML files to create a big XML in memory how this could be altered.

Anyway, here is a link to the example.


You can see the XML file at


If you start looking at the code know that the perl script just takes the entered text, reverses it and outputs the XML file.

Also know that there is only one XML file so if multiple people are using it, it might be garbled since I'm not locking anything.

I did it using zXml which you can get from


And the code is based on the examples in his book Professional Ajax. The library is a nice little system that is cross-browser yet looks a lot like doing it native so you get some feel for how it really works behind the scenes without having to mess with completely different systems.


sir pannels
12-07-2006, 09:43 AM
thanks for the reply and your time both of you.

David, something I do not understanding, you said it is streaming with a 5 second delay. If there was no delay, would it cause any problems?

I will look at your code today. thanks for that.

Also thanks for the COMET link, unfortunately im unable to use that on the current device.

cheers again

sir pannels
12-07-2006, 09:43 AM
hang on, professional ajax? I have that book but can't find the streaming chapter?

Also after trying your code, it works in IE but not Opera, do we know why? I'm after a solution for opera eek

12-07-2006, 06:04 PM
Well I guess it depends what you mean by streaming. If you mean taking a large file and getting bits of it at a time, then no, that isn't streaming. If you mean getting small bits of information "constantly" then yes that is it. Techically it can be implemented in a similar way but I'm not aware of anything supported in ajax that allows for the first way. At least not in a cross-browser solution.

What effect are you trying to get? Are you trying to load a big file in pieces and show it as it is loaded? Are you just trying to have "instant" updates from the changed xml file on your server? And I'm not sure what javascript tripping over itself means.

As for Opera, I don't know. It is supposed to work I think but I really haven't tested much with it.


12-11-2006, 12:00 PM
Hey guys

The script errors in opera due to the conditional check to create the __initError__() method on line 154:

if (oMozDocument && !window.opera) { I'd guess because opera didn't start supporting xmlhttp til recently ;-) Change the line to
if (oMozDocument && (window.opera && typeof XMLHttpRequest == "object")) {and it should be groovy.

As david says, it's really the type of "streaming" that you wish to occur - you can push and pull along the http request by keeping the connection alive to the webserver, not closing it down as soon as it's served the initially requested content.

COMET does enable data to be pushed unbroken to the client (a true "streaming" solution) - but you've mentioned that there's limitations on using a COMET server? There are many available for different platforms - COMETd, Pjax, fJax to name but a few.

David's solution is by far the best and easiest if you can: to have the client keep asking for a small file from the server and then have a server-side script keep updating that file. But it's not true streaming, - to do that requires keeping the connection to the server alive - using COMET and AJAX, or perhaps using macromedia flash streaming (http://www.adobe.com/support/flash/how/subjects/Stream1/).

The big question is what data are you streaming? If you can, I'd really go for david's option.

Hope this helps.