首页 > 后端开发 > php教程 > php读取xml的类

php读取xml的类

WBOY
发布: 2016-07-25 09:04:08
原创
1300 人浏览过
  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),' if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
  34. while(list($attr_name, $attr_value) = each($data["$key attr"]))
  35. echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
  36. reset($data["$key attr"]);
  37. }
  38. if(is_null($value)) echo " />\n";

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

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

应用实例,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.'页
');
  • $item=$item['item'];

  • echo('

  • ');
  • echo('

  • ');
  • foreach($item as $list)

  • {
  • echo('
  • ');
  • echo('
  • ');
  • echo('
  • ');
  • echo('
  • ');
  • echo('
  • ');
  • }
  • echo('

  • ');
  • echo('

  • 分类 标题 链接
    '.$list['sort'].' '.$list['title'].' '.$list['link'].'
  • ');
  • if($page>1) echo('上一页 ');
  • if($page下一页');
  • echo('
  • ');
  • ?>
  • 复制代码

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



    相关标签:
    来源:php.cn
    本站声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板