Home >php教程 >php手册 >详细解读PHP解析XML元素结构的代码示例

详细解读PHP解析XML元素结构的代码示例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 11:08:431102browse

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元素结构的具体分析,希望大家能够充分掌握。


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