国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP中使用SimpleXML檢查XML文件結構實例

2024-05-04 23:29:52
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了PHP中使用SimpleXML檢查XML文件結構實例,本文講解使用SimpleXML來檢查一個XML文件是否符合規范的方法,需要的朋友可以參考下
 

利用 SimpleXML 去檢查 XML 結構是否符合規格,為了讓這個程序可以多用途,采用了一個基準文件的作為結構準則,依據里面定義的節點跟屬性,去檢查文件是否符合基本要求的格式。

 

復制代碼代碼如下:

<?php    
    
/**檢查 XML 文件結構   
* @param string $baseFilePath 基準結構文件   
* @param string $checkFilePath 待檢查文件   
* @return bool 當結構與基準文件相符合時則傳遞 true,否則是 false   
* */    
function checkXmlFileStructure($baseFilePath,$checkFilePath){    
   /*開啟 Base File*/    
   if(!file_exists($baseFilePath)){ return false; }    
   $base = simplexml_load_file($baseFilePath);    
   if($base===false){ return false; }    
    
   /*開啟 Check File*/    
   if(!file_exists($checkFilePath)){ return false; }    
   $check = simplexml_load_file($checkFilePath);    
   if($check===false){ return false; }    
    
   /*比較起始點的名稱*/    
   if($base->getName() != $check->getName()){ return false; }    
    
   /*比較結構*/    
   return checkXmlStructure($base,$check);    
}    
    
/**檢查 XML 結構   
* @param SimpleXMLElement $base 基準結構對象   
* @param SimpleXMLElement $check 待檢查 XML 對象   
* @return bool 當結構與基準對象相符合時則傳遞 true,否則是 false   
* */    
function checkXmlStructure($base,$check){    
   /*檢查屬性*/    
   foreach ($base->attributes() as $name => $baseAttr){    
       /*必要的屬性不存在*/    
       if(!isset($check->attributes()->$name)){ return false; }    
   }    
    
   /*當沒有子節點時,則檢查對象也不能有子節點*/    
   if(count($base->children())==0){    
       return (count($check->children())==0);    
   }    
    
   /*將檢查對象的子節點分群*/    
   $checkChilds = array();    
   foreach($check->children() as $name => $child){    
       $checkChilds[$name][] = $child;    
   }    
    
   /*檢查子節點*/    
   $checked = array();    
   foreach($base->children() as $name => $baseChild){    
       /*跳過已經檢查的子節點*/    
       if(in_array($name, $checked)){ continue; }    
       $checked[] = $name;    
    
       /*檢查必要的子節點是否存在*/    
       if(emptyempty($checkChilds[$name])){ return false; }    
    
       foreach ($checkChilds[$name] as $child){    
           /*遞回檢查子節點*/    
           if( !checkXmlStructure($baseChild, $child) ){ return false; }    
       }    
   }    
    
   return true;    
}    
    
    
/*==============================================================================*/    
    
if(isset($_SERVER['argv'])){    
   parse_str(preg_replace('/&[/-]+/','&',join('&',$_SERVER['argv'])), $_GET);    
    
   if(emptyempty($_GET['base_file']) || emptyempty($_GET['check_file'])){    
       echo "Run: ".basename(__FILE__)." base_file=base.xml check_file=check.xml/n"; exit(1);    
   }    
    
   exit( checkXmlFileStructure($_GET['base_file'],$_GET['check_file']) ? 0 : 1);    
    
}else{    
   if(emptyempty($_GET['base_file']) || emptyempty($_GET['check_file'])){    
       echo "Run: ".basename(__FILE__)."?base_file=base.xml&check_file=check.xml<br />"; exit;    
   }    
    
   echo( checkXmlFileStructure($_GET['base_file'],$_GET['check_file']) ? '1' : '0');    
}   

 

使用方式(shell)

復制代碼代碼如下:

php check_xml_file_structure.php base_file=base.xml check_file=check.xml    
    
if [ "j$?" != "j0" ]; then    
   echo "Run Error"    
fi  

測試范例 1
base_1.xml
復制代碼代碼如下:

<?xml version="1.0" encoding="UTF-8"?>    
<items>    
   <item>    
       <Category>Category文字</Category>    
       <Title>Title文字</Title>    
   </item>    
</items>  
check_1.xml
 
<?xml version="1.0" encoding="UTF-8"?>    
<items>    
   <item>    
       <Category>Category文字</Category>    
       <Title>Title文字</Title>    
   </item>    
   <item>    
       <Category>Category文字</Category>    
       <Title>Title文字</Title>    
       <Description>Description文字</Description>    
   </item>    
</items>   

測試范例 2
base_2.xml
復制代碼代碼如下:

<?xml version="1.0" encoding="UTF-8"?>    
<items>    
   <item category="Category文字" style="padding: 0px 0px 20px; margin: 0px; clear: both; overflow: hidden; width: 680px; color: rgb(0, 0, 0); font-family: Tahoma, Helvetica, Arial, 宋體, sans-serif; background-color: rgb(247, 252, 255);">  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汽车| 汝阳县| 扎兰屯市| 清苑县| 内丘县| 普安县| 布尔津县| 饶平县| 锡林郭勒盟| 玉溪市| 华亭县| 中西区| 余庆县| 永川市| 中江县| 高唐县| 游戏| 台前县| 桓仁| 宽甸| 桐柏县| 延津县| 桃园市| 凤翔县| 广平县| 穆棱市| 阜新市| 新郑市| 宁津县| 临安市| 黄石市| 闽清县| 兴安盟| 天祝| 陆川县| 巴马| 高安市| 营口市| 清水县| 曲松县| 宜君县|