PHP uses SimpleXML to check whether the XML file structure is valid.-PHP source code

Source: Internet
Author: User
The SimpleXML function allows you to convert XML into objects. This object can be processed by using a common attribute selector or array iterator, just like any other object. SimpleXML functions are a core component of PHP. These functions can be used without installation. The SimpleXML function allows you to convert XML into objects. This object can be processed by using a common attribute selector or array iterator, just like any other object. SimpleXML functions are a core component of PHP. These functions can be used without installation.

Script ec (2); script

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:
/** 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. xmln"; 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:
Php check_xml_file_structure.php base_file = base. xml check_file = check. xml

If ["j $? "! = "J0"]; then
Echo "Run Error"
Fi

Test Example 1
Base_1.xml

The Code is as follows:



Category text
Title text

Check_1.xml

The Code is as follows:



Category text
Title text


Category text
Title text
Description text

Test Example 2
Base_2.xml

The Code is as follows:



Check_2.xml

The Code is as follows:





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.