iphone development blog

Tuesday, February 19, 2008 @ 1:16 AM

Part 5: Importing Data From an XML File

iPhone Minds Developer's Journal - Crossword Puzzle
Most programs, including games, require data. More often than not this data must be updated regularly so as to sustain the usefulness of the application. This type of data is usually stored externally (out of the application's main code) since it makes very little sense to require the application's user to download a new version of the program every day in order to receive the latest and greatest data. Therefore, with respect to our game, it makes a great deal of sense to store the data for each crossword puzzle in an external XML file. This way puzzles can easily be loaded individually by referencing a different XML file.

To import data from an XML file, we use the XMLHttpRequest Javascript object as follows:

var url = 'xml/070814.xml';
var xmlhttp = new XMLHttpRequest();

if (xmlhttp != null) {
xmlhttp.onreadystatechange = stateChange;
xmlhttp.open('GET', url, true);
xmlhttp.send(null);
} else {
alert('Your browser does not support XMLHTTP.'); }
}

function stateChange() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
/*
A status of 200 means that the XML file was
loaded and parsed without error. At this point
you'll want to sort through all of the data so
that it can be used by your program. I'll give
you some tips on how to do this tomorrow.
*/
} else {
alert('Problem retrieving XML data.');
}
}
}

We'll talk a little more about reading from XML files tomorrow.

// Ryan Jennings

Labels: , , , , ,


MindComet at 1:16 AM - View Post