Javascript Feature: Read Xml String, Parse, And Set Elements To Values

Javascript Feature: Read Xml String, Parse, And Set Elements To Values

JavaScript Feature: Read XML String, Parse, and Set Elements to Values

Written by: Robert Guida,, ©2009.

Who is this for?

Getting a fully functional script that actually reads in XML, parses it, and then sets element values is not easy to find on the Web. I searched for hours, but was only able to find bits and pieces to put together a function. So if you are looking to have a PHP script create an XML document filled with variables and HTML, then this code is for you.

What are all the files I need to evaluate this code?

The parts involved in this example code are a PHP file, “AJAX.php”, and an HTML file, “AJAX.htm”, with a few JavaScript functions.


PHP and JavaScript Coordination

There is some coordination between the PHP file, and the JavaScript funciton setElementValues(). The tags that hold the key information in the XML document are “formID” and “formVal”. More appropriate names would be elementID and elementValue, since these nodes cooresponde to the elements. The setElementValues() function gets the values of these two nodes, and based on the value for formID, that element in the HTML document will have its innerHTML set to the value found in the formVal node. If element name does not match the formID value, then the value is read, but never set.

Demo and Download

O.K. That is a lengthy intro, and if you are like me you just want the code, and to see an example. If you feel like you need some explanation, then you RTFM (Read The Freaking Manual). So let’s get to the good stuff… the example. Below is some HTML with a few element tags. Click the “Run AJAX XML Script” and see what happens. After that, down load the script, and open it in your editor.

This form will not work in this article. Please use the links at the bottom of the article to use the function examples.


E-Mail Receipt


If you have any questions, please do not hesitate to contact me, click here. I will do my best to get back to you promptly.Take care and good luck in your endevours.Rob, 2009

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *