PHP uses simplexml to examine XML file structure instances, simplexmlxml_php tutorial

Source: Internet
Author: User
Tags benchmark

PHP uses SimpleXML to check the XML file structure instance, Simplexmlxml


Using SimpleXML to check whether the XML structure conforms to specifications, in order to make the program multi-purpose, using a benchmark file as a structural criterion, according to the definition of the node and attributes, to check whether the file conforms to the basic requirements of the format.

Copy CodeThe code is as follows:
<?php

/** checking the XML file structure
* @param string $baseFilePath datum structure file
* @param string $checkFilePath Pending File
* @return BOOL Pass True if the structure matches the benchmark file, otherwise false
* */
function Checkxmlfilestructure ($baseFilePath, $checkFilePath) {
/* Open Base file*/
if (!file_exists ($baseFilePath)) {return false;}
$base = Simplexml_load_file ($baseFilePath);
if ($base ===false) {return false;}

/* Turn on Check file*/
if (!file_exists ($checkFilePath)) {return false;}
$check = Simplexml_load_file ($checkFilePath);
if ($check ===false) {return false;}

/* Compare the starting point name */
if ($base->getname ()! = $check->getname ()) {return false;}

/* Compare structure */
Return Checkxmlstructure ($base, $check);
}

/** Checking the XML structure
* @param simplexmlelement $base Datum Structure Object
* @param simplexmlelement $check The XML object to be examined
* @return BOOL Pass True if the structure matches the Datum object, otherwise false
* */
function Checkxmlstructure ($base, $check) {
/* Check Properties */
foreach ($base->attributes () as $name = = $baseAttr) {
/* The necessary attributes do not exist */
if (!isset ($check->attributes () $name)) {return false;}
}

/* When there are no child nodes, the check object also cannot have child nodes */
if (Count ($base->children ()) ==0) {
Return (COUNT ($check->children ()) ==0);
}

/* Sub-nodes of the check object are grouped */
$checkChilds = Array ();
foreach ($check->children () as $name = = $child) {
$checkChilds [$name] = $child;
}

/* Check child nodes */
$checked = Array ();
foreach ($base->children () as $name = = $baseChild) {
/* Skip the child nodes that have been checked */
if (In_array ($name, $checked)) {continue;}
$checked [] = $name;

/* Check if the necessary child nodes are present */
if (Emptyempty ($checkChilds [$name])) {return false;}

foreach ($checkChilds [$name] as $child) {
/* Recursively check child nodes */
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 ');
}

How to use (shell)
Copy CodeThe 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
Copy CodeThe 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
Copy CodeThe code is as follows:
<?xml version= "1.0" encoding= "UTF-8"?>



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





http://www.bkjia.com/PHPjc/939421.html www.bkjia.com true http://www.bkjia.com/PHPjc/939421.html techarticle PHP uses SimpleXML to check XML file structure instances, Simplexmlxml use SimpleXML to check whether the XML structure conforms to specifications, in order to make this program can be multi-purpose, the use of a benchmark ...

  • 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.