详细解读PHP解析XML元素结构的代码示例_PHP教程
时间:2021-07-01 10:21:17
帮助过:3人阅读
PHP解析XML元素结构的代码:
- < ?php
- $file = "data.xml";
- $depth = array();
- function startElement
($parser, $name, $attrs) { - global $depth;
- for ($i = 0; $i < $depth
[$parser]; $i++) { - print " ";
- }
- print "$namen";
- $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);
- ?>
以上代码示例就是PHP解析XML元素结构的具体分析,希望大家能够充分掌握。
http://www.bkjia.com/PHPjc/446188.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446188.htmlTechArticlePHP解析XML元素结构的代码: ?php $ file = data.xml ; $ depth = array (); functionstartElement ($parser,$name,$attrs){ global$depth; for($ i = 0 ;$i $depth [$parser];$i++){...