Read From XML Using Javascript

by aherrick 8. August 2008 07:08

Today I was working on some code to monitor my network speed (incoming and outgoing).  I wrote a windows service to capture my bandwidth and dynamically create an xml file and upload it to my website.  Every 30 seconds (time is totally configurable) I override the exisiting XML with an XML containing a fresh capture of my bandwidth speed.  How I wote the service is another post for another day...  However I do want to share some code I wrote to load the XML and update the text.

Here is an example of the XML file my windows service generates.

bandwidth xml

The XML file is named bandwidth.xml

I wrote a javascript function to attempt to read the XML file when it is in the same directory as my html file. See javascript function parseXML below.

bandwidth javascript

At the end of the function I make use of setTimeout to call the function again the function after a period of time.  In this case every 10 seconds.

I load the Javascript function using the body onload event.

body onload="parseXML()"

I have three span tags in my html.  XML, upload, and download.  I grab their IDs in the function and use innerHTML to replace the text.  Try it out by loading the html in a browser and changing the xml values.  Have some fun with it!

Check out the XML and HTML below to see the full code.  Good luck!


bandwidth.xml (100.00 bytes)

index.html (1.43 kb)

Tags: , ,



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen
Tweaked by ajondeck.NET

About the author

Andrew Herrick
I am a full time developer for Blue Horseshoe Solutions based out of Carmel, Indiana. I enjoy learning new technology and hope to give back with some of the cool things I pick up along the way.  Check me out on Stack Overflow!


Categories

None

Recent comments

Comment RSS

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008