This article describes how to use SimpleXML to check the XML file structure in PHP. This article describes how to use SimpleXML to check whether an XML file meets the standards. For more information, see
This article describes how to use SimpleXML to check the XML file structure in PHP. This article describes how to use SimpleXML to check whether an XML file meets the standards. For more information, see
SimpleXML is used to check whether the XML structure meets the specifications. To enable this program to be versatile, a reference file is used as the structure criterion based on the nodes and attributes defined in it, check whether the file meets the basic requirements.
The Code is as follows:
<? Php
/** Check the XML file structure
* @ Param string $ baseFilePath reference structure file
* @ Param string $ checkFilePath file to be checked
* @ Return bool: If the structure matches the baseline file, true is passed; otherwise, false is used.
**/
Function checkXmlFileStructure ($ baseFilePath, $ checkFilePath ){
/* Enable Base File */
If (! File_exists ($ baseFilePath) {return false ;}
$ Base = simplexml_load_file ($ baseFilePath );
If ($ base = false) {return false ;}
/* Enable Check File */
If (! File_exists ($ checkFilePath) {return false ;}
$ Check = simplexml_load_file ($ checkFilePath );
If ($ check = false) {return false ;}
/* Compare the start point name */
If ($ base-> getName ()! = $ Check-> getName () {return false ;}
/* Comparison structure */
Return checkXmlStructure ($ base, $ check );
}
/** Check the XML structure
* @ Param SimpleXMLElement $ base structure object
* @ Param SimpleXMLElement $ check the XML object to be checked
* @ Return bool: If the structure matches the benchmark object, true is passed; otherwise, false is used.
**/
Function checkXmlStructure ($ base, $ check ){
/* Check attributes */
Foreach ($ base-> attributes () as $ name => $ baseAttr ){
/* The required attribute does not exist */
If (! Isset ($ check-> attributes ()-> $ name) {return false ;}
}
/* If no subnode exists, the check object cannot have a subnode */
If (count ($ base-> children () = 0 ){
Return (count ($ check-> children () = 0 );
}
/* Group the child nodes of the check object */
$ CheckChilds = array ();
Foreach ($ check-> children () as $ name => $ child ){
$ CheckChilds [$ name] [] = $ child;
}
/* Check the subnode */
$ Checked = array ();
Foreach ($ base-> children () as $ name => $ baseChild ){
/* Skip the child node that has been checked */
If (in_array ($ name, $ checked) {continue ;}
$ Checked [] = $ name;
/* Check whether necessary subnodes exist */
If (emptyempty ($ checkChilds [$ name]) {return false ;}
Foreach ($ checkChilds [$ name] as $ child ){
/* Return to check the subnode */
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 ');
}
Usage (shell)
The Code is as follows: