栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php的XML文件解释类应用实例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php的XML文件解释类应用实例

本文实例讲述了php的XML文件解释类及其用法,是非常实用的技巧。分享给大家供大家参考。具体如下:

XMLParser.class.php类文件如下:

parser($xmlstring); 
    return $this->response($flag, $data); 
  } 
 
   
  public function loadXmlString($xmlstring){ 
    // parser xml 
    list($flag, $data) = $this->parser($xmlstring); 
    return $this->response($flag, $data); 
  } 
 
   
  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); 
  } 
 
   
  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; 
    } 
  } 
 
   
  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; 
 } 
      } 
    } 
  } 
 
   
  private function response($flag=false, $data=array()){ 
    return array($flag, $data); 
  } 
} 
?>

Demo示例程序如下:

 
 
1000 
 
100 
fdipzone 
1 
28 
 
'; 
echo '
'; 
$xml_parser = new XMLParser(); 
echo "response xmlfilern"; 
list($flag, $xmldata) = $xml_parser->loadXmlFile($xmlfile); 
if($flag){ 
  print_r($xmldata); 
} 
echo "response xmlstringrn"; 
list($flag, $xmldata) = $xml_parser->loadXmlString($xmlstring); 
if($flag){ 
  print_r($xmldata); 
} 
echo '
'; ?>

关于PHP的XML预定义常量可参考官方文档:
http://www.php.net/manual/en/libxml.constants.php

希望本文所述对大家PHP程序设计的学习有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/47327.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号