PHP中使用SimpleXML檢查XML檔案結構執行個體,simplexmlxml_PHP教程

來源:互聯網
上載者:User

PHP中使用SimpleXML檢查XML檔案結構執行個體,simplexmlxml


利用 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
"; 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"?>


Category文字
Title文字


check_1.xml

<?xml version="1.0" encoding="UTF-8"?>


Category文字
Title文字


Category文字
Title文字
Description文字



測試範例 2
base_2.xml
複製代碼 代碼如下:
<?xml version="1.0" encoding="UTF-8"?>



check_2.xml
<?xml version="1.0" encoding="UTF-8"?>





http://www.bkjia.com/PHPjc/939421.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/939421.htmlTechArticlePHP中使用SimpleXML檢查XML檔案結構執行個體,simplexmlxml 利用 SimpleXML 去檢查 XML 結構是否符合規格,為了讓這個程式可以多用途,採用了一個基準...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.