首頁  >  文章  >  後端開發  >  php讀取xml的類

php讀取xml的類

WBOY
WBOY原創
2016-07-25 09:04:081225瀏覽
  1. #-------------------- -- --------------------

  2. #
  3. # XML 函式庫,作者:Keith Devens,版本1.2b
  4. # http://keithdevens.com/software/phpxml
  5. #
  6. # 該程式碼是開源的,並根據類似於藝術授權的條款發布。
  7. # 閱讀http://keithdevens.com/software/license 上的許可證
  8. #
  9. #---------- ----------------------------------------------
  10. # XML_unserialize:將原始XML 作為參數(字串)
  11. # 並傳回等效的PHP 資料結構
  12. #-------------------- --- --------------------
  13. 函數& XML_unserialize(&$xml){
  14. $xml_parser = &new XML();
  15. $data = & $xml_parser->parse($xml);
  16. $xml_parser->destruct();
  17. 回傳$資料;
  18. }
  19. --------------- --------######
  20. # XML_serialize:將任何PHP 資料結構序列化為XML
  21. # 採用一個參數:要序列化的資料。必須是一個數組。
  22. ------------------------#
  23. 函數& XML_serialize(&$data, $level = 0, $prior_key = NULL){
  24. if($level == 0){ ob_start(); echo '',"n"; }
  25. while(list($key, $value) = every ($data))
  26. if(!strpos($key, ' attr')) #if 它不是一個屬性
  27. #我們不' t 單獨處理屬性,因此對於具有屬性的空元素
  28. # ,您仍然需要將元素設為NULL
  29. ; if(is_array($value) 和array_key_exists(0, $value)){

  30. XML_serialize($value, $level, $key );
  31. }其他{
  32. $tag = $prior_key ? $prior_key : $key;
  33. echo str_repeat("t", $level),' if(array_key_exists ("$key attr", $data)){ #如果該元素有屬性
  34. while(list($attr_name, $attr_value) =each($data["$key attr") ]))
  35. echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
  36. 重設($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. 重置( $資料);
  43. if($level == 0){ $str = &ob_get_contents(); ob_end_clean();回傳$str; }
  44. }
  45. #---------- --------------#
  46. # XML 類別:與PHP 的XML 處理函數一起使用的實用程式類別
  47. #------------ ------------------------#
  48. class XML{
  49. var $parser; #對XML 解析器的參考
  50. var $ document; #到目前為止構建的整個XML 結構
  51. var $parent; #指向當前父級的指針- 父級將是一個數組
  52. var $stack; # 每個嵌套級別的最新父級的堆疊
  53. var $last_opened_tag; #追蹤最後打開的標籤。
  54. 函數XML(){

  55. $this->parser = &xml_parser_create();
  56. xml_parser_set_option(&$this->解析器, XML_OPTION_CASE_FOLDING, false); (&$this->解析器, &$this);
  57. xml_set_element_handler(&$this->parser, 'open','close');
  58. xml_set_character_data_handler(&$this->parser, 'data' );
  59. }
  60. function destruct(){ xml_parser_free(&$this->parser); }
  61. 函數& parse(&$data){
  62. $this->document = array();
  63. $this->stack = array();
  64. $this->parent = &$this->document;
  65. return xml_parse(&$this->parser, &$data, true) ? $ this->文檔:NULL;
  66. }
  67. function open(&$parser, $tag, $attributes){
  68. $this->data = ''; #儲存臨時cdata
  69. $this- >last_opened_tag = $tag;
  70. if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #如果你之前看過這個標籤
  71. if(is_array($this ->parent) parent[$tag]) 和array_key_exists(0,$this->parent[$tag])){ #如果鍵是數字
  72. #這是我們遇到的$tag 的第三個或更高版本
  73. $key = count_numeric_items($this->parent[$tag]);
  74. }else{
  75. #這是我們看到的 $tag 的第二個實例。左右移動
  76. if(array_key_exists("$tag attr",$this->parent)){
  77. $arr = array('0 attr'=>&$this->parent["$tag attr"] , &$this->parent[$tag]);
  78. unset($this->parent["$tag attr"]);
  79. }else{
  80. $arr = array(&$this- >parent[$tag]);
  81. }
  82. $this->parent[$tag] = &$arr;
  83. $key = 1;
  84. }
  85. $this->parent = &$this->parent[$tag];
  86. }else{
  87. $key = $tag;
  88. }
  89. if($attributes) $this->parent["$key attr"] = $attributes;
  90. $this->parent = &$this->parent[$key];
  91. $this->stack[] = &$this->parent;
  92. }
  93. function data(&$parser, $data){
  94. if($this->last_opened_tag != NULL) #你不需要在標籤之間儲存空格
  95. $this -> 資料.= $資料;
  96. }
  97. function close(&$parser, $tag){
  98. if($this->last_opened_tag == $tag){
  99. $this->parent = $this->;資料;
  100. $this->last_opened_tag = NULL;
  101. }
  102. array_pop($this->stack);
  103. if($this->stack) $this->parent = &$this->stack[count( $this->stack)-1];
  104. }
  105. }
  106. 函數count_numeric_items(&$array){
  107. 回傳is_array($array) ?計數(array_filter(array_keys($array), 'is_numeric')) : 0;
  108. }
  109. ?>
複製代碼

應用實例,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; p>

  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新增資料一例 實例學習php操作XML的類別DOMDocument



    陳述:
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn