当前位置:Gxlcms > PHP教程 > php读取xml的类

php读取xml的类

时间:2021-07-01 10:21:17 帮助过:31人阅读

  1. #------------------------------------------

  2. #
  3. # XML Library, by Keith Devens, version 1.2b
  4. # http://keithdevens.com/software/phpxml
  5. #
  6. # This code is Open Source, released under terms similar to the Artistic License.
  7. # Read the license at http://keithdevens.com/software/license
  8. #
  9. #------------------------------------------
  10. # XML_unserialize: takes raw XML as a parameter (a string)
  11. # and returns an equivalent PHP data structure
  12. #------------------------------------------
  13. function & XML_unserialize(&$xml){
  14. $xml_parser = &new XML();
  15. $data = &$xml_parser->parse($xml);
  16. $xml_parser->destruct();
  17. return $data;
  18. }
  19. -----------------------#####
  20. # XML_serialize: serializes any PHP data structure into XML
  21. # Takes one parameter: the data to serialize. Must be an array.
  22. -----------------------#
  23. function & XML_serialize(&$data, $level = 0, $prior_key = NULL){
  24. if($level == 0){ ob_start(); echo '',"\n"; }
  25. while(list($key, $value) = each($data))
  26. if(!strpos($key, ' attr')) #if it's not an attribute
  27. #we don't treat attributes by themselves, so for an empty element
  28. # that has attributes you still need to set the element to NULL

  29. if(is_array($value) and array_key_exists(0, $value)){

  30. XML_serialize($value, $level, $key);
  31. }else{
  32. $tag = $prior_key ? $prior_key : $key;
  33. echo str_repeat("\t", $level),'<',$tag;
  34. if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
  35. while(list($attr_name, $attr_value) = each($data["$key attr"]))
  36. echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
  37. reset($data["$key attr"]);
  38. }

  39. if(is_null($value)) echo " />\n";

  40. elseif(!is_array($value)) echo '>',htmlspecialchars($value),"\n";
  41. else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"\n";
  42. }
  43. reset($data);
  44. if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
  45. }
  46. #-----------------------#
  47. # XML class: utility class to be used with PHP's XML handling functions
  48. #-----------------------#
  49. class XML{
  50. var $parser; #a reference to the XML parser
  51. var $document; #the entire XML structure built up so far
  52. var $parent; #a pointer to the current parent - the parent will be an array
  53. var $stack; #a stack of the most recent parent at each nesting level
  54. var $last_opened_tag; #keeps track of the last tag opened.

  55. function XML(){

  56. $this->parser = &xml_parser_create();
  57. xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);
  58. xml_set_object(&$this->parser, &$this);
  59. xml_set_element_handler(&$this->parser, 'open','close');
  60. xml_set_character_data_handler(&$this->parser, 'data');
  61. }
  62. function destruct(){ xml_parser_free(&$this->parser); }
  63. function & parse(&$data){
  64. $this->document = array();
  65. $this->stack = array();
  66. $this->parent = &$this->document;
  67. return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;
  68. }
  69. function open(&$parser, $tag, $attributes){
  70. $this->data = ''; #stores temporary cdata
  71. $this->last_opened_tag = $tag;
  72. if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
  73. if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
  74. #this is the third or later instance of $tag we've come across
  75. $key = count_numeric_items($this->parent[$tag]);
  76. }else{
  77. #this is the second instance of $tag that we've seen. shift around
  78. if(array_key_exists("$tag attr",$this->parent)){
  79. $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
  80. unset($this->parent["$tag attr"]);
  81. }else{
  82. $arr = array(&$this->parent[$tag]);
  83. }
  84. $this->parent[$tag] = &$arr;
  85. $key = 1;
  86. }
  87. $this->parent = &$this->parent[$tag];
  88. }else{
  89. $key = $tag;
  90. }
  91. if($attributes) $this->parent["$key attr"] = $attributes;
  92. $this->parent = &$this->parent[$key];
  93. $this->stack[] = &$this->parent;
  94. }
  95. function data(&$parser, $data){
  96. if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
  97. $this->data .= $data;
  98. }
  99. function close(&$parser, $tag){
  100. if($this->last_opened_tag == $tag){
  101. $this->parent = $this->data;
  102. $this->last_opened_tag = NULL;
  103. }
  104. array_pop($this->stack);
  105. if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
  106. }
  107. }
  108. function count_numeric_items(&$array){
  109. return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
  110. }
  111. ?>

应用实例,XML来源地址:http://data.cnaz.com/spread/?tid=7&sid=42845&order=date&flags=desc&num=50 &page=1&hosts=pic.ttiankan.com&code=xml

  1. include('xml.php'); //引用PHP XML操作类

  2. $page=$_GET['page'];

  3. if(empty($page) || !is_numeric($page)) $page=1;

  4. //偶像明星

  5. $xml = file_get_contents('http://data.cnaz.com/spread/?tid=7&sid=42845&order=date&flags=desc&num=50
  6. &page='.$page.'&hosts=pic.ttiankan.com&code=xml');

  7. //$xml = file_get_contents("php://input"); //读取POST过来的输入流
  8. $data=XML_unserialize($xml);

  9. $item=$data['rss']['channel'];

  10. $link = $item['link'];

  11. $description= $item['description'];
  12. $keywords = $item['keywords'];
  13. $totalnum = $item['totalnum'];
  14. $usetime = $item['usetime'];

  15. $totalpage=ceil($totalnum/50);

  16. echo('关键字:'.$keywords.' ');

  17. echo('总记录数:'.$totalnum.', 每页显示50条,一共有'.$totalpage.'页, 当前是第'.$page.'页');

  18. $item=$item['item'];

  19. echo('

  20. ');

  21. echo('

  22. ');

  23. foreach($item as $list)

  24. {
  25. echo('
  26. ');
  27. echo('
  28. ');
  29. echo('
  30. ');
  31. echo('
  32. ');
  33. echo('
  34. ');
  35. }

  36. echo('

  37. ');

  38. echo('

  39. 分类标题链接
    '.$list['sort'].''.$list['title'].''.$list['link'].'
  40. ');
  41. if($page>1) echo('上一页 ');
  42. if($page<$totalpage) echo('下一页');
  43. echo('
  44. ');
  45. ?>

脚本学堂编辑为您推荐: php操作xml的实例代码 php读写xml文件的方法介绍 php使用函数方式输出XML文件的例子 php使用函数方式读取XML文件的例子 PHP读取XML的几种方法 PHP读取XML值的代码 php操作xml的各种方法解析 PHP生成xml文件的4种方法 php生成xml简单实例详解 php向xml中添加数据一例 实例学习php操作XML的类DOMDocument

人气教程排行