PHP/XML/SAX

Материал из Web эксперт
Версия от 07:08, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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



SAX parser in action

<?
function start_element($parser, $element_name, $element_attrs) {
  switch ($element_name) {
    case "KEYWORDS":
       echo "<h1>Keywords</h1><ul>";
       break;
    case "KEYWORD":
       echo "<li>";
       break;
  }
}
function end_element($parser, $element_name) {
  switch ($element_name) {
    case "KEYWORDS":
       echo "</ul>";
       break;
    case "KEYWORD":
       echo "</li>";
       break;
  }
}

function character_data($parser, $data) {
  echo htmlentities($data);
}
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "end_element");
xml_set_character_data_handler($parser, "character_data");
$fp = fopen("keyword-data.xml", "r")
    or die ("Cannot open keyword-data.xml!");
    
    
while ($data = fread($fp, 4096)) {
  xml_parse($parser, $data, feof($fp)) or 
    die(sprintf("XML ERROR: %s at line %d",
        xml_error_string(xml_get_error_code($parser)),
        xml_get_current_line_number($parser)));
}

xml_parser_free($parser);    
?>

<!-- keyword-data.xml
<keywords>
  <keyword>XML</keyword>
  <keyword>PHP</keyword>
  <keyword>Perl</keyword>
  <keyword>JavaScript</keyword>
  <keyword>ASP</keyword>
</keywords>
-->



Turning an XML Document into an Array

<!--
<contact id="43956">
     <personal>
          <name>
               <first>J</first>
               <middle>J</middle>
               <last>J</last>
          </name>
          <title>Manager</title>
          <employer>National</employer>
          <dob>1971-12-22</dob>
     </personal>
</contact>
-->
<html>
<body>
<?php
     $cdata = "";
     $contact_id = -1;
     $contacts = null;
   
     function start_elem ($parser, $name, $attrs) {
          global $contact_id;
          if ($name == "contact") {
               $contact_id = $attrs["id"];
               $contacts[$contact_id] = array();
          }
     }
     function end_elem ($parser, $name) {
          global $cdata, $contacts, $contact_id;
          switch ($name)
          {
               case ("first"):
                    $contacts[$contact_id]["name"]["first"] = $cdata; 
                    break;
               case ("last"):
                    $contacts[$contact_id]["name"]["last"] = $cdata; 
                    break;
               case ("middle"):
                    $contacts[$contact_id]["name"]["middle"] = $cdata; 
                    break;
               case ("title"):
                    $contacts[$contact_id]["title"] = $cdata; 
                    break;
               case ("employer"):
                    $contacts[$contact_id]["employer"] = $cdata; 
                    break;
               case ("dob"):
                    $contacts[$contact_id]["dob"] = $cdata; 
                    break;
          }
          $cdata = "";
     }
     function cdata ($parser, $data) {
          global $cdata;
          $cdata .= trim ($data);
     }
     $file = "contact.xml";
     $parser = xml_parser_create ();
     xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, false);
     xml_set_element_handler ($parser, "start_elem", "end_elem");
     xml_set_character_data_handler ($parser, "cdata");
     $fp = fopen ($file, "r");
     if (!$fp){
          die ("Couldn"t open $file for reading");
     }
     while ($xml_input = fread ($fp, 2048)) {
          $ok = xml_parse ($parser, $xml_input, feof ($fp));
          if (!$ok){
               die (sprintf ("Error in $file: "%s" at line %d",
                             xml_error_string (xml_get_error_code ($parser)),
                             xml_get_current_line_number ($parser)));
          }
     }
     xml_parser_free ($parser);
     print ("<pre>\n");
     print_r ($contacts);
     print ("</pre>\n");
?>
</body>
</html>