Home >Backend Development >PHP Tutorial >PHP解析XML 元素结构范例_PHP

PHP解析XML 元素结构范例_PHP

WBOY
WBOYOriginal
2016-06-01 12:29:51796browse

  该范例用缩进格式显示一个文档中起始元素的结构。

  显示 XML 元素结构

$file = "data.xml";
$depth = array();

function startElement($parser, $name, $attrs) {
global $depth;
for ($i = 0; $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);
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn