PHPBuilder - Dynamic XML with PHP Page 2 Page 2



RSS Twitter
Articles

Dynamic XML with PHP Page 2 - Page 2

by: Ben Robinson
|
July 10, 2008

Download the code sample and look at the books.js file (inside books_example folder). These items are on lines 78-82. keep in mind that when you run the parse_xml function, you are already in a success block for an XMLhttpRequest:
if (xmlHttp.status == 200) { } 
so when you run the parse_xml function, you have the XML to parse:
var XMLResponse = XMLHttp.responseXML;
For the dynamic parsing part, we use yet another nested loop, exactly like we handled the XML generation from the database side. But first, we need to initialize our columnArray to the individual values they possess in the XML document. The values that are looped through are the column names passed into the function:
var columnArray = new Array(columns.length);
	//initialiaze element array to all values in DOM
   for (var i=0; i
Then we do another loop to prepare the XML, which needs to know how many of each item there are:
  // generate HTML output
  var html = "";  
  // iterate through the first DOM element array as an index
  for (var i=0; i
once we are in this nested loop, we just need to be careful of our indicis:
value = columnArray[c].item(i).firstChild.data;
Take special note of how the indexing is occurring here. The line above is the key to this function, for it is here that you will get your data (I had quite a hard time with this, you can see my commented out alert snippets throughout the code that you can use for debugging).

Notice the difficult syntax that native JavaScript has for this: having it inside this nested iterator will save you quite a bit of typing, and will avoid having to use any large include files or libraries to keep your application very lightweight.

//alert(value);
html += "<b>" + columns[c] + ": </b>" + value + "<br/>";
Now that we have our value, the line above is where you would apply any styling information, or dress up your html code. For just looking at the basic data, all I've done is bold the column name, and echo out the value, so your output would look something like this:
Fieldname: value
However, what if you want to use the php method to do your field mapping for you? All it needs is a slight mod to print out your JavaScript for you:
function gen_jsFieldlist($tableName) {
	
$this->set_fields($tableName);
$outputjs = "<script>\nvar columns = new Array(".count($this->fieldnames).");\n";
	$index = 0;
	
	foreach ($this->fieldnames as $value) {
		$outputjs .= 'columns['.$index.'] = "'.$value.'";';
		$outputjs .= "\n";
		$index++;
		}
		
	$outputjs .= "</script>\n";
	echo $outputjs;
	}
Now all you have to do on your page load is call that method b/w your head tags and it will print out a nice script block for you on your page when it loads. In conclusion, we have looked at several methods for handling XML both from a database side and from a parsing side using AJAX. Here are some additional thoughts on these techniques:
What if you don't always want every column name in a table to end up in your output XML? Suggestion: change the get_item() method to use your shortened fieldnames array instead of setting the fieldnames list using the method set_fields. There are many ways to go about this, but if you want a very dynamic application, you would have to touch all three methods: set_fields(), get_item(), and gen_xml(), or at least have them all read from that shortened fieldlist. One way to handle this easily would be to make the array accessible to all these methods.

What if you wanted to make the JavaScript parse_xml() function more intelligent to just autoparse each node in the XML? i.e if you tried the suggestion above and already knew you were preparing the proper XML in the php code?

suggestion 1: Experiment with the responseXML object in JavaScript to see if you can extract the fieldnames right out of the XML document using DOM techniques. You could then use this as your columns array.

suggestion 2: Print out for your first record in the XML, a field array from the php side. Parse the array into a JavaScript array when you retrieve the XML. You could also save yourself a line of code in JavaScript if you also return the amount of records (we did however, propose one solution to this, with the gen_jsFieldlist method).

Credits
The books.js demo was modified from the sample code in the book AJAX and PHP: Building Responsive Web Applications. Christian Darie, one of the authors, has a great page for the book with the code examples here.
Ben Robinson is an open source coder who enjoys working on dynamic web applications, especially in PHP and mysql, in the L.A.M.P. environment. His website is TierraLogic Systems (http://www.tierralogic.com/) if you'd like to pay him a visit.



« Previous Page
1

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Ben Robinson

Comment:



Comment:

(Maximum characters: 1200). You have characters left.