Top Banner
PHP and XML
29

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.

Dec 24, 2015

Download

Documents

Kory Payne
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

PHP and XML

Page 2: 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.

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.

Page 3: 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.

Select XML file from dialog box

Page 4: 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.

An error already?

What is this root element anyway?

Page 5: 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.

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.

Page 6: 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.

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.

Page 7: 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.

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

Page 8: 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/Save As…

Page 9: 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.

View/Data Grid

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

Page 10: 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.

Enter data into the Data Grid

Page 11: 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.

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

Page 12: 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.

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

Page 13: 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.

Code view: inside the select

Page 14: 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.

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

Page 15: 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.

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

Page 16: 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.

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++;

}}

Page 17: 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.

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;

}}

Page 18: 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.

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;

}}

}

Page 19: 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.

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

Page 20: 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.

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

*/

Page 21: 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.

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)));

}

Page 22: 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.

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

*/

Page 23: 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.

Finishing up

fclose($fp);

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

xml_parser_free($xml_parser);

//free up resources used by parser

Page 24: 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.

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

Page 25: 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.

Not displaying old things

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

Page 26: 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.

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

);

Page 27: 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.

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.

Page 28: 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.

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.

Page 29: 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.

No old training showing

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