Recent Changes - Search:

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.

Parse XML documents with PHP

Overview: 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

  • Create a folder named xml-tut in the document root of the Web server (i.e. C:/xampp/htdocs/xml-tut)
  • Create the following simple XML document and save it as xml-fruit.xml.
<?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

  • Create the following PHP program and save it as parse-xml-1.php in the same folder you placed xml-fruit.xml.
      $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);
  • Use your browser to execute the PHP program by the URL: localhost/xml/parse-xml-1.php
(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

  • Write a PHP program that can parse a XML Sitemap and output a HTML that displays all the links like the following.
      <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>
  • The XML Sitemap for your testing will be provided.

Edit - History - Print - Recent Changes - Search
Page last modified on January 26, 2011, at 06:11 PM