日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

xml解析类

發(fā)布時(shí)間:2023/12/9 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 xml解析类 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)載鏈接:http://zyan.cc/post/253


今天在PHP4環(huán)境下重新寫一個(gè)接口程序,需要大量分析解析XML,PHP的xml_parse_into_struct()函數(shù)不能直接生成便于使用的數(shù)組,而SimpleXML擴(kuò)展在PHP5中才支持,于是逛逛搜索引擎,在老外的網(wǎng)站上找到了一個(gè)不錯(cuò)的PHP XML操作類。

一、用法舉例:
1、將XML文件解釋成便于使用的數(shù)組:

<?php include('xml.php'); //引用PHP XML操作類 $xml = file_get_contents('data.xml'); //讀取XML文件 //$xml = file_get_contents("php://input"); //讀取POST過來的輸入流 $data=XML_unserialize($xml); echo '<pre>'; print_r($data); echo '</pre>'; ?>
data.xml文件: <?xml version="1.0" encoding="GBK"?> <video> <upload> <videoid>998</videoid> <name><![CDATA[回憶未來]]></name> <memo><![CDATA[def]]></memo> <up_userid>11317</up_userid> </upload> </video>
利用該XML操作類生成的對(duì)應(yīng)數(shù)組(漢字編碼:UTF-8): Array ( [video] => Array ( [upload] => Array ( [videoid] => 998 [name] => 回憶未來 [memo] => def [up_userid] => 11317 ) ) )
2、將數(shù)組轉(zhuǎn)換成XML文件: <?php include('xml.php');//引用PHP XML操作類 $xml = XML_serialize($data); ?>

二、PHP XML操作類源代碼: <?php ################################################################################### # # XML Library, by Keith Devens, version 1.2b # <a href="http://keithdevens.com/software/phpxml" target="_blank">http://keithdevens.com/software/phpxml</a> # # This code is Open Source, released under terms similar to the Artistic License. # Read the license at <a href="http://keithdevens.com/software/license" target="_blank">http://keithdevens.com/software/license</a> # ################################################################################### ################################################################################### # XML_unserialize: takes raw XML as a parameter (a string) # and returns an equivalent PHP data structure ################################################################################### function & XML_unserialize(&$xml){ $xml_parser = &new XML(); $data = &$xml_parser->parse($xml); $xml_parser->destruct(); return $data; } ################################################################################### # XML_serialize: serializes any PHP data structure into XML # Takes one parameter: the data to serialize. Must be an array. ################################################################################### function & XML_serialize(&$data, $level = 0, $prior_key = NULL){ if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; } while(list($key, $value) = each($data)) if(!strpos($key, ' attr')) #if it's not an attribute #we don't treat attributes by themselves, so for an emptyempty element # that has attributes you still need to set the element to NULL if(is_array($value) and array_key_exists(0, $value)){ XML_serialize($value, $level, $key); }else{ $tag = $prior_key ? $prior_key : $key; echo str_repeat("\t", $level),'<',$tag; if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element while(list($attr_name, $attr_value) = each($data["$key attr"])) echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"'; reset($data["$key attr"]); } if(is_null($value)) echo " />\n"; elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n"; else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n"; } reset($data); if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; } } ################################################################################### # XML class: utility class to be used with PHP's XML handling functions ################################################################################### class XML{ var $parser; #a reference to the XML parser var $document; #the entire XML structure built up so far var $parent; #a pointer to the current parent - the parent will be an array var $stack; #a stack of the most recent parent at each nesting level var $last_opened_tag; #keeps track of the last tag opened. function XML(){ $this->parser = &xml_parser_create(); xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false); xml_set_object(&$this->parser, &$this); xml_set_element_handler(&$this->parser, 'open','close'); xml_set_character_data_handler(&$this->parser, 'data'); } function destruct(){ xml_parser_free(&$this->parser); } function & parse(&$data){ $this->document = array(); $this->stack = array(); $this->parent = &$this->document; return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL; } function open(&$parser, $tag, $attributes){ $this->data = ''; #stores temporary cdata $this->last_opened_tag = $tag; if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric #this is the third or later instance of $tag we've come across $key = count_numeric_items($this->parent[$tag]); }else{ #this is the second instance of $tag that we've seen. shift around if(array_key_exists("$tag attr",$this->parent)){ $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]); unset($this->parent["$tag attr"]); }else{ $arr = array(&$this->parent[$tag]); } $this->parent[$tag] = &$arr; $key = 1; } $this->parent = &$this->parent[$tag]; }else{ $key = $tag; } if($attributes) $this->parent["$key attr"] = $attributes; $this->parent = &$this->parent[$key]; $this->stack[] = &$this->parent; } function data(&$parser, $data){ if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags $this->data .= $data; } function close(&$parser, $tag){ if($this->last_opened_tag == $tag){ $this->parent = $this->data; $this->last_opened_tag = NULL; } array_pop($this->stack); if($this->stack) $this->parent = &$this->stack[count($this->stack)-1]; } } function count_numeric_items(&$array){ return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0; } ?>

總結(jié)

以上是生活随笔為你收集整理的xml解析类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产a黄| 人妖交videohd另类 | 99久久国产精 | 免费精品| 精品人妻一区二区三区视频 | 波多野结衣视频免费在线观看 | www,xxx日本 | 成人免费看aa片 | 久久久久久一区 | 国模大尺度视频 | 成年人小视频在线观看 | 色伊人av| 国产精品99精品无码视亚 | 女女互慰揉小黄文 | 免费看黄色片网站 | 亚洲熟妇av乱码在线观看 | 欧美在线xxx | 国产日韩欧美一区二区 | 中文字幕一区二区不卡 | 丁香色欲久久久久久综合网 | 免费污视频| youjizz.com国产| 天天综合网天天综合 | 777四色 | 欧美毛片网站 | 草草影院第一页 | 免费看久久| 日日夜精品 | 一级伦理农村妇女愉情 | 在线观看av网页 | 超碰自拍 | 青青视频网站 | 久久精品人人爽 | 波多野结衣电车 | 欧美va天堂 | www.xxx亚洲 | 婷婷tv| 成人影 | 国产永久在线观看 | 日韩成人激情 | 女女同性女同一区二区三区九色 | 国精产品一区二区三区 | 色亚洲色图 | 亚洲色图第一页 | 91免费高清 | 在线国产观看 | 亚洲精品乱码久久久久久国产主播 | 亚洲天堂一 | www一级片| 成人免费高清在线观看 | 日韩一级伦理片 | 久久久久久久久亚洲 | 在线观看亚洲网站 | 欧美xxx视频 | 国产一区二区三区四区五区在线 | 日韩精品视频在线看 | 91免费黄视频 | 三级网站在线 | 91精品国产欧美一区二区 | 久久综合成人网 | 日韩91精品 | 熟妇女人妻丰满少妇中文字幕 | 欧美日韩国产成人精品 | 熟女自拍一区 | 精品无码在线观看 | 欧美国产高潮xxxx1819 | 制服丝袜第二页 | 国产精品无人区 | 精品韩国一区二区三区 | 国产内射一区二区 | 久久夫妻视频 | 男人操女人逼逼视频 | 欧美一级大片在线观看 | 欧美性大战久久久久久 | 久久久永久久久人妻精品麻豆 | 国产视频精品免费 | 亚洲色图另类小说 | 91精品国产91久久久久久久久久久久 | 天天综合网天天综合 | 九九视频在线观看 | 国产精品欧美激情在线播放 | 色婷婷aⅴ | 亚洲熟女乱色一区二区三区久久久 | 亚洲一级电影 | 91波多野结衣| 99国产精品99久久久久久 | 亚洲操| 日本天堂免费a | 亚洲永久在线观看 | 国产91影院 | 69av片| 91丨porny丨成人蝌蚪 | 落日余晖 | 精品国产999 | 人妻偷人精品一区二区三区 | 黄色网页在线 | 亚洲 欧美 激情 小说 另类 | 日日夜夜免费 | 麻豆精品一区二区三区 |