顯示 XML 元素結構
<?PHP
$file = "data.XML";
$depth = array();
function startElement($parser, $name, $attrs) {
global $depth;
for ($i = 0; $i < $depth[$parser]; $i++) {
print " ";
}
print "$name\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);
?>