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++){...
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