《PHP教程:PHP中使用SimpleXML檢查XML文件結構實例》要點:
本文介紹了PHP教程:PHP中使用SimpleXML檢查XML文件結構實例,希望對您有用。如果有疑問,可以聯系我們。
利用 SimpleXML 去檢查 XML 結構是否符合規格,為了讓這個程序可以多用途,采用了一個基準文件的作為結構準則,依據里面定義的節點跟屬性,去檢查文件是否符合基本要求的格式.PHP教程
代碼如下:
<?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教程
代碼如下:
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筆墨" Title="Title筆墨"/>???
</items>??
check_2.xml
<?xml version="1.0" encoding="UTF-8"?>???
<items>???
?? <item category="Category筆墨" Title="Title筆墨" Description="Description筆墨" />???
?? <item category="Category筆墨" Title="Title筆墨" />???
?? <item category="Category筆墨" Title="Title筆墨" Description="Description筆墨" />???
</items>
《PHP教程:PHP中使用SimpleXML檢查XML文件結構實例》是否對您有啟發,歡迎查看更多與《PHP教程:PHP中使用SimpleXML檢查XML文件結構實例》相關教程,學精學透。維易PHP學院為您提供精彩教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/12814.html