A simple XML parser : SAX « XML « PHP






A simple XML parser

<?php
$file = "contact.xml";
   
function startElement($parser, $name, $attrs) {
    print "<B>$name =></B>  ";
}

function endElement($parser, $name) {
    print "\n";
}
   
function characterData($parser, $value) {
    print "$value<BR>";
}
   
$simpleparser = xml_parser_create();
xml_set_element_handler($simpleparser, "startElement", "endElement");
xml_set_character_data_handler($simpleparser, "characterData");
   
if (!($fp = fopen($file, "r"))) {
  die("could not open XML input");
}
   
while($data = fread($fp, filesize($file))) {
  if (!xml_parse($simpleparser, $data, feof($fp))) {
     die(xml_error_string(xml_get_error_code($simpleparser)));
  }
}

xml_parser_free($simpleparser);
?>
<!--
<contact id="43956">
     <personal>
          <name>
               <first>J</first>
               <middle>J</middle>
               <last>J</last>
          </name>
          <title>Manager</title>
          <employer>National Company</employer>
          <dob>1951-02-02</dob>
     </personal>
</contact>

-->

           
       








Related examples in the same category

1.SAX parser in action
2.Turning an XML Document into an Array