PREVIOUS UP   Technologies Internet et Education, © TECFA
  5. "Stream-parsing"

5. "Stream-parsing"

En général, on définit des "handlers" qui réagissent aux différentes événements. PHP supporte plusieurs fonctions pour activer des "handlers" comme par ex:

Php a aussi plusieurs constantes pour les erreurs:

XML_ERROR_NONE, XML_ERROR_NO_MEMORY, XML_ERROR_SYNTAX, XML_ERROR_NO_ELEMENTS, XML_ERROR_INVALID_TOKEN, XML_ERROR_UNCLOSED_TOKEN, XML_ERROR_PARTIAL_CHAR, XML_ERROR_TAG_MISMATCH, XML_ERROR_DUPLICATE_ATTRIBUTE ...

Exemple 5-1: La structure d'un fichier xml en forme d'une liste des éléments

 
<?
$file = "choco-chip.xml";
 
$depth = array();
# trouve le début d'une balise et imprime-la
function startElement($parser, $name, $attrs)
{
    global $depth;
    for ($i = 0; $i < $depth[$parser]; $i++) {
        print "&nbsp;&nbsp;&nbsp;";
    }
    print "$name\n<br>";
    $depth[$parser]++;
}
# trouve la fin d'une balise et imprime-le
function endElement($parser, $name)
{
    global $depth;
    $depth[$parser]--;
    for ($i = 0; $i < $depth[$parser]; $i++) {
        print "&nbsp;&nbsp;&nbsp;";
    }
    print "/$name\n<br>";
}
 
# créer un "parser"
$xml_parser = xml_parser_create();
 
# commence à "parser" 
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
}
 
# libérer le "parser"
xml_parser_free($xml_parser);
 
?>

Exemple 5-2: Visualisation des données xml en html

 
<?
$file = "choco-chip.xml";
 
// Put the key in upper case !
$begin_array = array(
           "LIST"        => "<H1>RECIPE LIST</H1>",
           "RECIPE"      => "<HR>",
           "RECIPE_NAME" => "<H2>",
           "AUTHOR"      => "Author: <STRONG>",
           "MEAL"        => "<H3>",
           "COURSE"      => "<I>",
           "INGREDIENTS" => "<H3>Ingrediants</H3><OL>",
           "ITEM"        => "<LI>",
           "DIRECTIONS"  => "<H3>Directions</H3><BLOCKQUOTE>"
           );
 
$end_array = array(
           "LIST"        => "<BR>",
           "RECIPE"      => "<BR>",
           "RECIPE_NAME" => "</H3>",
           "AUTHOR"      => "</STRONG>",
           "MEAL"        => "</H2>",
           "COURSE"      => "</I>",
           "INGREDIENTS" => "</OL>",
           "ITEM"        => "</LI>",
           "DIRECTIONS"  => "</BLOCKQUOTE>"
           );
 
function startElement($parser, $name, $attrs)
{
    global $begin_array;
    // print "DEBUG: $name <br>";
    if ($htmlexpr = $begin_array[$name]) {
        print "$htmlexpr";
    }
}
 
function endElement($parser, $name)
{
    global $end_array;
    if ($htmlexpr = $end_array[$name]) {
        print "$htmlexpr";
    }
}

 
function characterData($parser, $data)
{
    print $data;
}
 
$xml_parser = xml_parser_create();
 
// use case-folding so we are sure to find the tag in $begin_array
// does this REALLY work ??? / DKS
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
 
if (!($fp = fopen($file, "r"))) {
  die("could not open XML input");
}
 
while ($data = fread($fp, 4096)) {
  if (!xml_parse($xml_parser, $data, feof($fp))) {
    die(sprintf("XML error: %s at line %d",
        xml_error_string(xml_get_error_code($xml_parser)),
        xml_get_current_line_number($xml_parser)));
  }
}
 
xml_parser_free($xml_parser);
 
?>

PREVIOUS UP -- TIE