php XML文件解释类
生活随笔
收集整理的這篇文章主要介紹了
php XML文件解释类
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
XMLParser.class.php
<?php /** XML 文件分析類(lèi) * Date: 2013-02-01 * Author: fdipzone * Ver: 1.0 * * func: * loadXmlFile($xmlfile) 讀入xml文件輸出Array * loadXmlString($xmlstring) 讀入xmlstring 輸出Array */class XMLParser{/** 讀取xml文件* @param String $xmlfile* @return Array*/public function loadXmlFile($xmlfile){// get xmlfile content$xmlstring = file_exists($xmlfile)? file_get_contents($xmlfile) : '';// parser xmllist($flag, $data) = $this->parser($xmlstring);return $this->response($flag, $data);}/** 讀取xmlstring* @param String $xmlstring* @return Array*/public function loadXmlString($xmlstring){// parser xmllist($flag, $data) = $this->parser($xmlstring);return $this->response($flag, $data);}/** 解釋xml內(nèi)容* @param String $xmlstring* @return Array*/private function parser($xmlstring){$flag = false;$data = array();// check xml formatif($this->checkXmlFormat($xmlstring)){$flag = true;// xml to object$data = simpleXML_load_string($xmlstring, 'SimpleXMLElement', LIBXML_NOCDATA);// object to array$this->objectToArray($data);}return array($flag, $data);}/** 檢查xml格式是否正確* @param String $xmlstring* @return boolean*/private function checkXmlFormat($xmlstring){if($xmlstring==''){return false;}$xml_parser_obj = xml_parser_create();if(xml_parse_into_struct($xml_parser_obj, $xmlstring, $vals, $indexs)===1){ // 1:success 0:failreturn true;}else{return false;}}/** object 轉(zhuǎn) Array* @param object $object* @return Array*/private function objectToArray(&$object){$object = (array)$object;foreach($object as $key => $value){if($value==''){$object[$key] = "";}else{if(is_object($value) || is_array($value)){$this->objectToArray($value);$object[$key] = $value;}}}}/** 輸出返回* @param boolean $flag true:false* @param Array $data 轉(zhuǎn)換后的數(shù)據(jù)* @return Array*/private function response($flag=false, $data=array()){return array($flag, $data);}}?>Demo:
<?php require "XMLParser.class.php";$xmlfile = 'file.xml'; $xmlstring = '<?xml version="1.0" encoding="utf-8"?> <xmlroot> <status>1000</status> <info></info> <result><id>100</id> <name>fdipzone</name> <gender>1</gender> <age>28</age> </result> </xmlroot>';echo '<pre>';$xml_parser = new XMLParser(); echo "response xmlfile\r\n"; list($flag, $xmldata) = $xml_parser->loadXmlFile($xmlfile); if($flag){print_r($xmldata); }echo "response xmlstring\r\n"; list($flag, $xmldata) = $xml_parser->loadXmlString($xmlstring); if($flag){print_r($xmldata); }echo '</pre>'; ?>
PHP XML預(yù)定義常量:?http://www.php.net/manual/en/libxml.constants.php
<?php /** XML 文件分析類(lèi) * Date: 2013-02-01 * Author: fdipzone * Ver: 1.0 * * func: * loadXmlFile($xmlfile) 讀入xml文件輸出Array * loadXmlString($xmlstring) 讀入xmlstring 輸出Array */class XMLParser{/** 讀取xml文件* @param String $xmlfile* @return Array*/public function loadXmlFile($xmlfile){// get xmlfile content$xmlstring = file_exists($xmlfile)? file_get_contents($xmlfile) : '';// parser xmllist($flag, $data) = $this->parser($xmlstring);return $this->response($flag, $data);}/** 讀取xmlstring* @param String $xmlstring* @return Array*/public function loadXmlString($xmlstring){// parser xmllist($flag, $data) = $this->parser($xmlstring);return $this->response($flag, $data);}/** 解釋xml內(nèi)容* @param String $xmlstring* @return Array*/private function parser($xmlstring){$flag = false;$data = array();// check xml formatif($this->checkXmlFormat($xmlstring)){$flag = true;// xml to object$data = simpleXML_load_string($xmlstring, 'SimpleXMLElement', LIBXML_NOCDATA);// object to array$this->objectToArray($data);}return array($flag, $data);}/** 檢查xml格式是否正確* @param String $xmlstring* @return boolean*/private function checkXmlFormat($xmlstring){if($xmlstring==''){return false;}$xml_parser_obj = xml_parser_create();if(xml_parse_into_struct($xml_parser_obj, $xmlstring, $vals, $indexs)===1){ // 1:success 0:failreturn true;}else{return false;}}/** object 轉(zhuǎn) Array* @param object $object* @return Array*/private function objectToArray(&$object){$object = (array)$object;foreach($object as $key => $value){if($value==''){$object[$key] = "";}else{if(is_object($value) || is_array($value)){$this->objectToArray($value);$object[$key] = $value;}}}}/** 輸出返回* @param boolean $flag true:false* @param Array $data 轉(zhuǎn)換后的數(shù)據(jù)* @return Array*/private function response($flag=false, $data=array()){return array($flag, $data);}}?>Demo:
<?php require "XMLParser.class.php";$xmlfile = 'file.xml'; $xmlstring = '<?xml version="1.0" encoding="utf-8"?> <xmlroot> <status>1000</status> <info></info> <result><id>100</id> <name>fdipzone</name> <gender>1</gender> <age>28</age> </result> </xmlroot>';echo '<pre>';$xml_parser = new XMLParser(); echo "response xmlfile\r\n"; list($flag, $xmldata) = $xml_parser->loadXmlFile($xmlfile); if($flag){print_r($xmldata); }echo "response xmlstring\r\n"; list($flag, $xmldata) = $xml_parser->loadXmlString($xmlstring); if($flag){print_r($xmldata); }echo '</pre>'; ?>
PHP XML預(yù)定義常量:?http://www.php.net/manual/en/libxml.constants.php
轉(zhuǎn)載于:https://www.cnblogs.com/fdipzone/archive/2013/02/02/3715139.html
總結(jié)
以上是生活随笔為你收集整理的php XML文件解释类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: PMAboutView
- 下一篇: Ural(Timus) 1081. B