PHP中使用SimpleXML檢查XML文件結(jié)構(gòu)實(shí)例
更新時(shí)間:2015年01月07日 09:40:49 投稿:junjie
這篇文章主要介紹了PHP中使用SimpleXML檢查XML文件結(jié)構(gòu)實(shí)例,本文講解使用SimpleXML來(lái)檢查一個(gè)XML文件是否符合規(guī)范的方法,需要的朋友可以參考下
利用 SimpleXML 去檢查 XML 結(jié)構(gòu)是否符合規(guī)格,為了讓這個(gè)程序可以多用途,采用了一個(gè)基準(zhǔn)文件的作為結(jié)構(gòu)準(zhǔn)則,依據(jù)里面定義的節(jié)點(diǎn)跟屬性,去檢查文件是否符合基本要求的格式。
復(fù)制代碼 代碼如下:
<?php
/**檢查 XML 文件結(jié)構(gòu)
* @param string $baseFilePath 基準(zhǔn)結(jié)構(gòu)文件
* @param string $checkFilePath 待檢查文件
* @return bool 當(dāng)結(jié)構(gòu)與基準(zhǔn)文件相符合時(shí)則傳遞 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; }
/*比較起始點(diǎn)的名稱*/
if($base->getName() != $check->getName()){ return false; }
/*比較結(jié)構(gòu)*/
return checkXmlStructure($base,$check);
}
/**檢查 XML 結(jié)構(gòu)
* @param SimpleXMLElement $base 基準(zhǔn)結(jié)構(gòu)對(duì)象
* @param SimpleXMLElement $check 待檢查 XML 對(duì)象
* @return bool 當(dāng)結(jié)構(gòu)與基準(zhǔn)對(duì)象相符合時(shí)則傳遞 true,否則是 false
* */
function checkXmlStructure($base,$check){
/*檢查屬性*/
foreach ($base->attributes() as $name => $baseAttr){
/*必要的屬性不存在*/
if(!isset($check->attributes()->$name)){ return false; }
}
/*當(dāng)沒(méi)有子節(jié)點(diǎn)時(shí),則檢查對(duì)象也不能有子節(jié)點(diǎn)*/
if(count($base->children())==0){
return (count($check->children())==0);
}
/*將檢查對(duì)象的子節(jié)點(diǎn)分群*/
$checkChilds = array();
foreach($check->children() as $name => $child){
$checkChilds[$name][] = $child;
}
/*檢查子節(jié)點(diǎn)*/
$checked = array();
foreach($base->children() as $name => $baseChild){
/*跳過(guò)已經(jīng)檢查的子節(jié)點(diǎn)*/
if(in_array($name, $checked)){ continue; }
$checked[] = $name;
/*檢查必要的子節(jié)點(diǎn)是否存在*/
if(emptyempty($checkChilds[$name])){ return false; }
foreach ($checkChilds[$name] as $child){
/*遞回檢查子節(jié)點(diǎn)*/
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)
復(fù)制代碼 代碼如下:
php check_xml_file_structure.php base_file=base.xml check_file=check.xml
if [ "j$?" != "j0" ]; then
echo "Run Error"
fi
測(cè)試范例 1
base_1.xml
復(fù)制代碼 代碼如下:
<?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>
測(cè)試范例 2
base_2.xml
復(fù)制代碼 代碼如下:
<?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使用DOM和simplexml讀取xml文檔的方法示例
- php使用simplexml_load_file加載XML文件并顯示XML的方法
- php+xml編程之SimpleXML的應(yīng)用實(shí)例
- php中simplexml_load_file函數(shù)用法實(shí)例
- php的SimpleXML方法讀寫XML接口文件實(shí)例解析
- php simplexmlElement操作xml的命名空間實(shí)現(xiàn)代碼
- PHP中simplexml_load_string函數(shù)使用說(shuō)明
- PHP XML操作的各種方法解析(比較詳細(xì))
- PHP中的生成XML文件的4種方法分享
- PHP基于SimpleXML生成和解析xml的方法示例
相關(guān)文章
php array_map與array_walk比較案例詳解
這篇文章主要介紹了php array_map與array_walk比較案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09使用Limit參數(shù)優(yōu)化MySQL查詢的方法
我們?cè)谧鲆恍┎樵兊臅r(shí)候總希望能避免數(shù)據(jù)庫(kù)引擎做全表掃描,因?yàn)槿頀呙钑r(shí)間長(zhǎng),而且其中大部分掃描對(duì)客戶端而言是沒(méi)有意義的。那么在 MySQL 中有那些方式是可以避免全表掃面的呢?除了我們大家很熟悉的通過(guò)使用索引列或分區(qū)等方式來(lái)進(jìn)行查詢的優(yōu)化之外還有那些呢?2008-11-11php生成百度sitemap站點(diǎn)地圖類函數(shù)實(shí)例
這篇文章主要介紹了php生成百度sitemap站點(diǎn)地圖類函數(shù)的方法,詳細(xì)講述了百度站點(diǎn)sitemap的實(shí)現(xiàn)方法與注意事項(xiàng),在web站點(diǎn)的建設(shè)中非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10基于PHP實(shí)現(xiàn)堆排序原理及實(shí)例詳解
這篇文章主要介紹了基于PHP實(shí)現(xiàn)堆排序原理及實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06