|
Network Programming ![]() This website demonstrates using wikis as teaching and learning tool. The course instructor is happy to share the teaching materials here with those who find it readable. |
Tutorial /
Parse XML documents with PHPOverview: In this tutorial, you will study how to use PHP to do event-based parsing of a XML documents.
Activity One: Install XAMPP as a Web server in your PC
Activity Two: Create a XML document
<?xml version="1.0"?> <fruits> <fruit>Apple</fruit> <fruit>Pear</fruit> <fruit>Peach</fruit> <fruit>Plum</fruit> <fruit>Cherry</fruit> <fruit>Blackberry</fruit> <fruit>Strawberry</fruit> <fruit>Blueberry</fruit> <fruit>Papaya</fruit> <fruit>Lemon</fruit> </fruits> Activity Three: Make a PHP program to parse the XML document
$file = "xml-fruit.xml";
function contents($parser, $data){
echo $data;
}
function startTag($parser, $data){
if($data=='FRUITS') echo "<ol>";
if($data=='FRUIT') echo "<li>";
}
function endTag($parser, $data){
if($data=='FRUITS') echo "</ol>";
if($data=='FRUIT') echo "</li>";
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($file, "r");
$data = fread($fp, 80000);
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
(Q1) Identify the PHP statements for the following functions/operations
01) Define the function that will be called when a start tag is reached.
02) Define the function that will be called when an end tag is reached.
03) Define the function that will be called when a data is reached.
04) Create the parser
05) Set the start and end tag handlers
06) Set the data handler
07) Open the XML file
08) Read the XML file
09) Parse the XML data
10) Destroy the parser
11) Close the XML file
Task Four: Parse a XML Sitemap documen
<html>
<head>
<title>Parse XML Sitemap</title>
</head>
<body>
<h1>Web pages indexed by the Search Engines</h1>
<ol>
<li><a href="a">a</a></li>
<li><a href="b">b</a></li>
<li><a href="c">c</a></li>
</ol>
</body>
</html>
|