PHP解析xml格式數(shù)據(jù)工具類示例
本文實(shí)例講述了PHP解析xml格式數(shù)據(jù)工具類。分享給大家供大家參考,具體如下:
class ome_xml { /** * xml資源 * * @var resource * @see xml_parser_create() */ public $parser; /** * 資源編碼 * * @var string */ public $srcenc; /** * target encoding * * @var string */ public $dstenc; /** * the original struct * * @access private * @var array */ public $_struct = array(); /** * Constructor * * @access public * @param mixed [$srcenc] source encoding * @param mixed [$dstenc] target encoding * @return void * @since */ function SofeeXmlParser($srcenc = null, $dstenc = null) { $this->srcenc = $srcenc; $this->dstenc = $dstenc; // initialize the variable. $this->parser = null; $this->_struct = array(); } /** * Parses the XML file * * @access public * @param string [$file] the XML file name * @return void * @since */ function xml2array($file) { //$this->SofeeXmlParser('utf-8'); $data = file_get_contents($file); $this->parseString($data); return $this->getTree(); } function xml3array($file){ $data = file_get_contents($file); $this->parseString($data); return $this->_struct; } /** * Parses a string. * * @access public * @param string data XML data * @return void */ function parseString($data) { if ($this->srcenc === null) { $this->parser = xml_parser_create(); } else { if($this->parser = xml_parser_create($this->srcenc)) { return 'Unable to create XML parser resource with '. $this->srcenc .' encoding.'; } } if ($this->dstenc !== null) { @xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $this->dstenc) or die('Invalid target encoding'); } xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); // lowercase tags xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1); // skip empty tags if (!xml_parse_into_struct($this->parser, $data, $this->_struct)) { /*printf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser) );*/ $this->free(); return false; } $this->_count = count($this->_struct); $this->free(); } /** * return the data struction * * @access public * @return array */ function getTree() { $i = 0; $tree = array(); $tree = $this->addNode( $tree, $this->_struct[$i]['tag'], (isset($this->_struct[$i]['value'])) ? $this->_struct[$i]['value'] : '', (isset($this->_struct[$i]['attributes'])) ? $this->_struct[$i]['attributes'] : '', $this->getChild($i) ); unset($this->_struct); return $tree; } /** * recursion the children node data * * @access public * @param integer [$i] the last struct index * @return array */ function getChild(&$i) { // contain node data $children = array(); // loop while (++$i < $this->_count) { // node tag name $tagname = $this->_struct[$i]['tag']; $value = isset($this->_struct[$i]['value']) ? $this->_struct[$i]['value'] : ''; $attributes = isset($this->_struct[$i]['attributes']) ? $this->_struct[$i]['attributes'] : ''; switch ($this->_struct[$i]['type']) { case 'open': // node has more children $child = $this->getChild($i); // append the children data to the current node $children = $this->addNode($children, $tagname, $value, $attributes, $child); break; case 'complete': // at end of current branch $children = $this->addNode($children, $tagname, $value, $attributes); break; case 'cdata': // node has CDATA after one of it's children $children['value'] .= $value; break; case 'close': // end of node, return collected data return $children; break; } } //return $children; } /** * Appends some values to an array * * @access public * @param array [$target] * @param string [$key] * @param string [$value] * @param array [$attributes] * @param array [$inner] the children * @return void * @since */ function addNode($target, $key, $value = '', $attributes = '', $child = '') { if (!isset($target[$key]['value']) && !isset($target[$key][0])) { if ($child != '') { $target[$key] = $child; } if ($attributes != '') { foreach ($attributes as $k => $v) { $target[$key][$k] = $v; } } $target[$key]['value'] = $value; } else { if (!isset($target[$key][0])) { // is string or other $oldvalue = $target[$key]; $target[$key] = array(); $target[$key][0] = $oldvalue; $index = 1; } else { // is array $index = count($target[$key]); } if ($child != '') { $target[$key][$index] = $child; } if ($attributes != '') { foreach ($attributes as $k => $v) { $target[$key][$index][$k] = $v; } } $target[$key][$index]['value'] = $value; } return $target; } /** * Free the resources * * @access public * @return void **/ function free() { if (isset($this->parser) && is_resource($this->parser)) { xml_parser_free($this->parser); unset($this->parser); } } }
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP針對(duì)XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP錯(cuò)誤與異常處理方法總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php獲取通過(guò)http協(xié)議post提交過(guò)來(lái)xml數(shù)據(jù)及解析xml
- php解析xml方法實(shí)例詳解
- php解析xml 的四種簡(jiǎn)單方法(附實(shí)例)
- PHP用SAX解析XML的實(shí)現(xiàn)代碼與問(wèn)題分析
- php遍歷解析xml字符串的方法
- php 解析xml 的四種方法詳細(xì)介紹
- PHP基于SimpleXML生成和解析xml的方法示例
- PHP使用xpath解析XML的方法詳解
- PHP處理數(shù)組和XML之間的互相轉(zhuǎn)換
- php實(shí)現(xiàn)將數(shù)組轉(zhuǎn)換為XML的方法
- PHP簡(jiǎn)單實(shí)現(xiàn)解析xml為數(shù)組的方法
相關(guān)文章
php實(shí)現(xiàn)Linux服務(wù)器木馬排查及加固功能
這篇文章主要介紹了php實(shí)現(xiàn)Linux服務(wù)器木馬排查及加固功能,本文給出了根據(jù)特征碼查找、搜索最近被修改的文件、修改php.ini、修改nginx.conf等方法,需要的朋友可以參考下2014-12-12php下判斷數(shù)組中是否存在相同的值array_unique
今天在改一個(gè)N久以前寫(xiě)的程序 突然碰到一個(gè)問(wèn)題 假設(shè)有一個(gè)數(shù)組$a中存在幾個(gè)value 我如何判斷這些value當(dāng)中是否存在相同的值呢? 翻了好多資料,也問(wèn)了兵哥哥,給我一些思路,想自己寫(xiě)來(lái)著~~~ 還是不肯放棄百度,最后搞了一次,居然找到這么一個(gè)函數(shù) array_unique爽大了。2008-03-03PHP字符串函數(shù)系列之nl2br(),在字符串中的每個(gè)新行 (\n) 之前插入 HTML 換行符br
nl2br() 函數(shù)在字符串中的每個(gè)新行 (\n) 之前插入 HTML 換行符 (br)。2011-11-11PHP Session變量不能傳送到下一頁(yè)的解決方法
在PHP中使用過(guò)SESSION的朋友可能會(huì)碰到這么一個(gè)問(wèn)題,SESSION變量不能跨頁(yè)傳遞。這令我苦惱了好些日子,最終通過(guò)查資料思考并解決了這個(gè)問(wèn)題。2009-11-11php判斷/計(jì)算閏年的方法小結(jié)【三種方法】
這篇文章主要介紹了php判斷/計(jì)算閏年的方法,結(jié)合實(shí)例形式總結(jié)分析了三種計(jì)閏年的判斷方法,需要的朋友可以參考下2019-07-07