Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

    Streaming XML with AJAX

    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 !

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    hi

    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

  • #3
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    You might want to have a look at an AJAX server push technology such as COMET. 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

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    http://www.exfer.net/test/streamed_xml_test.html

    You can see the XML file at

    http://www.exfer.net/test/streamed_xml_test.xml

    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

    http://www.nczonline.net/downloads/

    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.

    david_kw

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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

  • #7
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    david_kw

  • #8
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Hey guys

    The script errors in opera due to the conditional check to create the __initError__() method on line 154:
    Code:
    if (oMozDocument && !window.opera) {
    I'd guess because opera didn't start supporting xmlhttp til recently ;-) Change the line to
    Code:
    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.

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

    Hope this helps.

    m_n
    Last edited by martin_narg; 12-11-2006 at 11:04 AM.
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •