IT360 Lab 10: Extensible Markup Language

 

DUE:Tuesday, April 28th, 2009, BEFORE start of lab

 

This lab should get you familiar with the creation and use of XML and XML-related files.  

 

PART 1: Reading and Writing an XML File

 

Create a folder on your W drive called Lab10.  In that folder, create a php file called writeNames.php with the following script.

 

<?php

//Identify file to be written

$File = "names.xml";

 

//Create a file handler – w+ to create file if doesn’t exist and overwrite contents

$fh = fopen($File, 'w+') or die ("there is a problem with the file") ;

 

//Write the xml header

fwrite($fh,"<?xml version = \"1.0\" ?>\n");

 

//Create the XML set

fwrite($fh, "<names>\n");

 

//Write the data – can be done in a loop with SQL and PHP

fwrite($fh, "\t<name>\n\t\t<first>Bobby</first>\n\t\t<last>Brown</last>\n</name>\n");

fwrite($fh, "\t<name>\n\t\t<first>Tracey</first>\n\t\t<last>Tanner</last>\n</name>\n");

 

//Close off XML set

fwrite($fh, "</names>");

 

//close file handler

fclose($fh);

?>

 

Run the script from your browser and check out the file called names.xml.  Create another php file called readNames.php with the following script.  Now that your XML file is loaded into an object ($xml), you can display simple values by using the function getName( )  or the field names themselves .

 

<?php

 

// set name of XML file

$file = "names.xml";

 

// load file

$xml = simplexml_load_file($file) or die ("Unable to load XML file!");

 

print_r($xml);  // take a look at what the xml file looks like as an array

 

echo "<h2>" . $xml->getName() . "</h2><br />"; // root = "Names"

 

foreach ($xml->name as $Name) {    // can use the actual field identifier, if known

      echo "<h2>first: " . $Name->first . "  last: " . $Name->last . "</h2>" ;

}

?>

 

Alternately, you can use the children( ) function to find child tags (ie. name is a child of names and first/last are children of name).  We can use a variable to represent the attributes or fields - $Attribute makes as much sense as anything else.  This means that we can use $Attribute->getName() to get the name of the attribute and $Attribute to get the value itself.

 

foreach($xml->children() as $Name){

    echo “<h2>”;

    foreach($Name->children() as $Attribute) {  // the child of a name is its attribute

              echo $Attribute->getName() . ": " . $Attribute . "\t";

    }

    echo "</h2>”;

}

 

·       Add another line to the writeNames.php file to have it write your name.  Run writeNames.php followed by readName.php.

 

   

PART 2: Creating XML Data Files from your MySQL database

 

Download the lab10.zip file from Blackboard.  If you don’t already have a songs table in your database, you can add one by loading songs.sql via a script.  Run this as a script in MySQL (File-> Open Script).  This will create and populate a song table in your database.  Make sure that you can run the song_page.php script by changing the parameters in the myConnectDB.php to match your database. 

 

·       Create a function in song.inc.php that writes all songs from your database (table songs) into XML format into a file called songs.xml.  Use what you learned in writeName.php above to implement the function.

 

      function write_songs_xml($db){

           

      }

 

·       Modify song_page.php to invoke the write_songs_xml( ) function any time a song is added, deleted or updated.  This will ensure that your external users have up-to-date access to your song collection.

 

This is how your songs.xml file might look like:

 

<Songs>

      <Song>

            <Artist>America</Artist>

            <Title>Horse with no name</Title>

      </Song>

      <Song>

            <Artist>Foo Fighters</Artist>

            <Title>Long Road to Ruin</Title>

      </Song>

      <Song>

            <Artist>Grateful Dead</Artist>

            <Title>Stairway to Heaven</Title>

      </Song>

</Songs>

 

 

PART 3: Displaying XML data into Tables

 

·       Now create a new php script called readSongs.php that will read the XML version of the song file and format the up-to-date XML data file into a well-formatted HTML table.  Use readNames.php as a template.

 

PART 4 (extra credit): Create an XSLT style sheet

 

  • Create a XSLT stylesheet called songs.xsl that will display the songs in the songs.xml file as an HTML table.
  • Modify the write_songs_xml() method in song.inc.php to link the stylesheet to the songs.xml file, so when displayed in the browser, the songs.xml file will be displayed as an HTML table.

 

 

Turn in (due before start of lab on April 28, 2009):

Electronic:

  1. Upload the files song.inc.php, song_page.php, songs.xml, readSongs.php and, for extra credit, the songs.xsl stylesheet to blackboard.

 

Hard-copies:

  1. The completed assignment coversheet. Your comments will help us improve the course.
  2. Hard copies of song.inc.php and readSongs.php.
  3. For extra credit, hard copy of songs.xsl.