PHP XML Parsing

<pre>

<?php
$file = "artikel.xml";
$depth = array();

function startElement($parser, $name, $attrs)
{
   global $depth;
   for ($i = 0; $i < $depth[$parser]; $i++) {
       echo "  ";
   }
   echo "$name ";

        if (sizeof($attrs))
        {
                while (list($k,$v) = each($attrs))
                {
//                      print "$k -> $v  --";
                }
                print $attrs['AUTHOR'];
        }

   echo "\n";
   $depth[$parser]++;
}

function endElement($parser, $name)
{
   global $depth;
   $depth[$parser]--;
}

$xml_parser = xml_parser_create();
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)));
   }
}
xml_parser_free($xml_parser);
?>

</pre>
Copyright(c) Unifix.org 2002-2011

 

Dette indlæg blev udgivet i Knowledge Base, Old Base, Programmering. Bogmærk permalinket.

Skriv et svar