Home > Backend Development > PHP Tutorial > Detailed interpretation of code examples for PHP parsing XML element structure_PHP Tutorial

Detailed interpretation of code examples for PHP parsing XML element structure_PHP Tutorial

WBOY
Release: 2016-07-15 13:32:03
Original
747 people have browsed it

PHP解析XML元素结构的代码:

  1. < ?php  
  2. $file = "data.xml";  
  3. $depth = array();  
  4. function startElement
    ($parser, $name, $attrs) {  
  5. global $depth;  
  6. for ($i = 0; $i < $depth
    [$parser]; $i++) {  
  7. print " ";  
  8. }  
  9. print "$namen";  
  10. $depth[$parser]++;  
  11. }  
  12. function endElement($parser, $name) {  
  13. global $depth;  
  14. $depth[$parser]--;  
  15. }  
  16. $xml_parser = xml_parser_create();  
  17. xml_set_element_handler($xml_parser,
     "startElement", "endElement");  
  18. if (!($fp = fopen($file, "r"))) {  
  19. die("could not open XML input");  
  20. }  
  21. while ($data = fread($fp, 4096)) {  
  22. if (!xml_parse($xml_parser,
     $data, feof($fp))) {  
  23. die(sprintf("XML error: %s at line %d",  
  24. xml_error_string(xml_get_
    error_code($xml_parser)),  
  25. xml_get_current_line_
    number($xml_parser)));  
  26. }  
  27. }  
  28. xml_parser_free($xml_parser);  
  29. ?> 

以上代码示例就是PHP解析XML元素结构的具体分析,希望大家能够充分掌握。


www.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++){...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template