**XMLParser.class.php**
~~~
<?php
/**XML 文件分析類
*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 xml
list($flag, $data) = $this->parser($xmlstring);
return $this->response($flag, $data);
}
/**讀取xmlstring
* @param String $xmlstring
* @return Array
*/
public function loadXmlString($xmlstring){
// parser xml
list($flag, $data) = $this->parser($xmlstring);
return $this->response($flag, $data);
}
/**解釋xml內容
* @param String $xmlstring
* @return Array
*/
private function parser($xmlstring){
$flag = false;
$data = array();
// check xml format
if($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:fail
return true;
}else{
return false;
}
}
/**object 轉 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 轉換后的數據
* @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預定義常量:?**[http://www.php.net/manual/en/libxml.constants.php](http://www.php.net/manual/en/libxml.constants.php)
- 前言
- php Captcha 驗證碼類
- php 替換敏感字符串
- php返回數據格式化類
- php XML文件解釋類
- php CSS Update Class
- PHPMailer - PHP email transport class
- PHP 遍歷文件夾及文件類及處理類
- 自動登入google play下載app report
- php click captcha 驗證碼類
- php 獲取頁面中的指定內容類
- php 支持斷點續傳的文件下載類
- php 縮略圖生成類,支持imagemagick及gd庫兩種處理
- php 根據url自動生成縮略圖
- php 過濾html標記屬性類
- php HTTP請求類,支持GET,POST,Multipart/form-data
- php Cookies 操作類
- php 密碼生成類
- php main 與 iframe 相互通訊類(同域/跨域)
- php 根據url自動生成縮略圖,并處理高并發問題
- php Timer 頁面運行時間監測類
- php 雙向隊列類
- php 導出CSV抽象類
- php zip文件內容比較類
- php 獲取/設置用戶訪問頁面語言類
- php 獲取Youtube某個User所有Video信息
- php 字符編碼轉換類,支持ANSI、Unicode、Unicode big endian、UTF-8、UTF-8+Bom 互相轉換
- php 版本處理類