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

Source: Internet
Author: User
Tags foreach valid

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: Copy code
<? 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. 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 <br/> "; exit;
   }   
   
Echo (checkXmlFileStructure ($ _ GET ['base _ file'], $ _ GET ['Check _ file'])? '1': '0 ');
}  

Usage (shell)

The code is as follows: Copy code
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: Copy code
<? Xml version = "1.0" encoding = "UTF-8"?>
<Items>
<Item>
<Category> Category text </Category>
<Title> Title text </Title>
</Item>
</Items>

Check_1.xml

The code is as follows: Copy code
<? Xml version = "1.0" encoding = "UTF-8"?>
<Items>
<Item>
<Category> Category text </Category>
<Title> Title text </Title>
</Item>
<Item>
<Category> Category text </Category>
<Title> Title text </Title>
<Description> Description </Description>
</Item>
</Items>

Test Example 2
Base_2.xml

The code is as follows: Copy code
<? Xml version = "1.0" encoding = "UTF-8"?>
<Items>
<Item category = "Category text" Title = "Title text"/>
</Items>

Check_2.xml

The code is as follows: Copy code
<? Xml version = "1.0" encoding = "UTF-8"?>
<Items>
<Item category = "Category text" Title = "Title text" Description = "Description text"/>
<Item category = "Category text" Title = "Title text"/>
<Item category = "Category text" Title = "Title text" Description = "Description text"/>
</Items>

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.