Use SimpleXML in PHP to check XML file structure instances

Source: Internet
Author: User
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 complies with the specifications, if you need it, you can refer to SimpleXML 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, check whether the file meets the basic requirements based on the nodes and attributes defined in the file.

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:


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:


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


Category text
Title text


Check_1.xml

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


Category text
Title text


Category text
Title text
Description text



Test Example 2
Base_2.xml

The code is as follows:


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



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




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.