29
PHP and XML

PHP and XML. Visual Studio/File/New/File I have switched to Visual Studio to make the XML – I think it provides an easier interface for creating an XML

Embed Size (px)

Citation preview

PHP and XML

Visual Studio/File/New/File

I have switched to Visual Studio to make the XML – I think it provides an easier interface for creating an XML file.

Select XML file from dialog box

An error already?

What is this root element anyway?

Recall what an HTML document looks like

It starts with the DOCTYPE tag that announce what version of HTML is being used. Then comes the <html> tag and its closing </html> tag which encloses the remainder of the document.

XML wants the equivalent of the <html> tag. However, since it’s XML which is defined by the user, it cannot provide this tag itself.

Type in a root element – this tag appears once and only once and contains everything but the <?xml tag

Studio provides the closing tag as soon as you finish the opening tag.

Type in your first set of elements – establishing your tags and placing some data within

File/Save As…

View/Data Grid

The result is reminiscent of an Excel spreadsheet and/or Access database and makes it convenient to add more data.

Enter data into the Data Grid

Click on the other tab to see the data in XML code

PHP page with empty drop-down list (<select> tag)

Code view: inside the select

XML Parser

• PHP provides an XML parser – a function that reads XML

• Because XML is user-defined, you must supply it with some information, such as – What kind of data should it expect– What should it do with the data

Establish some variable for the xml parsing $insideitem = false; //Boolean that determines is we are

//within <training> tags$tag = ""; //holds tag data to determine what kind of

//xml tag we are dealing with $code=""; //used to hold the code data $title=""; //used to hold the title data$date=""; //used to hold the date data$time=""; //used to hold the time data$location=""; //used to hold the location data$description=""; //used to hold the description data$itemCount=0; //used to count the number of training items

startElement function

/*Function parser will use to recognize the start of an element*/function startElement($parser, $tagName, $attrs){

global $insideitem, $tag, $code, $title, $date, $time, $location, $description, $itemCount;

if($insideitem) //if we are inside a training item, we should be dealing with internal tags {

$tag=$tagName;}elseif(strtolower($tagName)=="training") //if we are not inside a training item,{

$insideitem=true;$itemCount++;

}}

endElement function

function endElement($parser, $tagName){

global $insideitem, $tag, $code, $title, $date, $time, $location, $description, $itemCount;

if(strtolower($tagName) == "training"){

print "<option value=\"" . $code . "\">"; //print option codeprint $title . "(" . $date . ")";print "</option>";

//clear out data $code = "";$title = "";$date = "";$time = "";$location = "";$description = "";$insideitem=false;

}}

function characterData($parser, $data){

global $insideitem, $tag, $code, $title, $date, $time, $location, $description, $itemCount;

if($insideitem){

switch(strtolower($tag)){

case "code“:$code .=$data;break;

case "title“:$title .=$data;break;

case "date“:$date .=$data;break;

case "time“:$time .=$data;break;

case "location“:$location .=$data;break;

case "description“:$description .=$data;break;

}}

}

Create xml parser

//PROGRAM STARTS HERE (above was functions)$xml_parser = xml_parser_create(); //create an xml parser, give it a name

xml_set_element_handler($xml_parser, "startElement", "endElement");

//informs parser of above functions so it can identify the data and //knows what to do with it

xml_set_character_data_handler($xml_parser, "characterData");//informs parser of above function

Specify the xml file to be read

$fp = fopen("ACT_Training.xml", "r");

/*

fp is a "file pointer" it says where a file is found. We have indicated a "relative address" which implies the xml file is in the same folder/directory as the php file "r" indicates the xml file will be read

*/

Reading xml file

while($data = fread($fp, 4096))

{

xml_parse($xml_parser, $data, feof($fp))

or die(sprintf("XML error: %s at line %d",

xml_error_string(xml_get_error_code($xml_parser)),

xml_get_current_line_number($xml_parser)));

}

Comment on reading xml file

/*Read data from the file in 4 kilobyte chunks. The while loop says that while there is still data to be read we should stay in the loop.

The xml_parse() function tells the parser we established to parse the data just read in. If the xml file was larger than 4KB, then certain closing tags might not be found in the data taken in so far. The feof($fp) seen

as the third argument of the xml_parse() lets the parser know whether the end-of-file has been reached. If the end-of-file has been reached and the appropriate closing has not been found, then there is a problem.

The xml_parse() function refers to the rules you establish in the startElement, endElement and characterData functions. The or part is used if the xml parsing goes wrong.

sprintf is a C-style print function the %s and %d indicate a string and a number that will follow

*/

Finishing up

fclose($fp);

//close the "connection/stream" to the data file

xml_parser_free($xml_parser);

//free up resources used by parser

Result: items in drop-down list coming from XML file

Not displaying old things

In this example the first training session in the XML file has passed.

Make an array that will convert months to numbers$months = array(

'Jan' => 1,'Feb' => 2,'Mar' => 3,'Apr' => 4,'May' => 5,'Jun' => 6,'Jul' => 7,'Aug' => 8,'Sep' => 9,'Oct' => 10,'Nov' => 11,'Dec' => 12

);

Use the split function to parse the date into separate $month, $day and $year variables

list($month, $day, $year) = split('[/.-]', $date);• The function split will break its second

argument into an array using its first argument as a delimiter or delimiters. – Here the delimiter could be a slash, a period

or a hyphen

• The list function allows you to take what would be an array and assign the values to individual variables.

Only show future training

$trainingTime = mktime(0,0,0,$months[$month], $day+1, $year);$now = time();

if($trainingTime >= $now){

print "<option value=\"" . $code . "\">";print $title . "(" . $date . ")";print "</option> \n";

}• Use the mktime() to make a time from the xml date data

(actually the very beginning of the next day), and make another time corresponding to right now.

• Only add the option to the drop-down list if the training is in the future.

No old training showing

The June 28th training is in the XML file, but was not placed in the drop-down list.