Code has been added to clipboard!

Initializing Expat XML Parser

Example
<?php
  // Initializing the XML Expat parser  
  $expat_parser = xml_parser_create(); 
  // Declare the function we will use at the beginning of the element
  function start($expat_parser,$_name,$_attrs) {    
 switch($_name) {     
      case "NOTE": 
        echo "-- Note --<br>";      
        break;      
      case "TO": 
        echo "Recipient: ";      
        break;      
      case "FROM": 
        echo "Sender: ";      
        break;      
      case "HEADING": 
        echo "Topic: ";      
        break;      
      case "BODY": 
        echo "Letter: ";
    }
  }
  //Declare the function we will use at the ending of the element
  function stop($expat_parser, $_name) {    
    echo "<br>";
  }
  //Declare the function we will use for finding character data
  function char($expat_parser, $data) {
    echo $data;
  }
  // Declare the handler for our elements
  xml_set_element_handler($expat_parser, "start", "stop");
  // Declare the handler for our data
  xml_set_character_data_handler($expat_parser, "char"); 
  // Open the XML file we want to read
  $fp = fopen("node.xml", "r");
  // Read our data
  while ($data=fread($fp,4096)) {
    xml_parse($expat_parser, $data, feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($expat_parser)), xml_get_current_line_number($expat_parser)));
  } 
  // Free/stop using the XML Expat Parser 
  xml_parser_free($expat_parser);  
?>