Download presentation
Presentation is loading. Please wait.
Published byKory Payne Modified over 9 years ago
1
PHP and XML
2
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.
3
Select XML file from dialog box
4
An error already? What is this root element anyway?
5
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 tag and its closing tag which encloses the remainder of the document. XML wants the equivalent of the tag. However, since it’s XML which is defined by the user, it cannot provide this tag itself.
6
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.
7
Type in your first set of elements – establishing your tags and placing some data within
8
File/Save As…
9
View/Data Grid The result is reminiscent of an Excel spreadsheet and/or Access database and makes it convenient to add more data.
10
Enter data into the Data Grid
11
Click on the other tab to see the data in XML code
12
PHP page with empty drop-down list ( tag)
13
Code view: inside the select
14
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
15
Establish some variable for the xml parsing $insideitem = false; //Boolean that determines is we are //within 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
16
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++; }
17
endElement function function endElement($parser, $tagName) { global $insideitem, $tag, $code, $title, $date, $time, $location, $description, $itemCount; if(strtolower($tagName) == "training") { print " ";//print option code print $title. "(". $date. ")"; print " "; //clear out data $code = ""; $title = ""; $date = ""; $time = ""; $location = ""; $description = ""; $insideitem=false; }
18
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; }
19
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
20
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 */
21
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))); }
22
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 */
23
Finishing up fclose($fp); //close the "connection/stream" to the data file xml_parser_free($xml_parser); //free up resources used by parser
24
Result: items in drop-down list coming from XML file
25
Not displaying old things In this example the first training session in the XML file has passed.
26
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 );
27
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.
28
Only show future training $trainingTime = mktime(0,0,0,$months[$month], $day+1, $year); $now = time(); if($trainingTime >= $now) { print " "; print $title. "(". $date. ")"; print " \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.
29
No old training showing The June 28 th training is in the XML file, but was not placed in the drop-down list.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.