Application Example of XML File Interpretation Class of php
- 2021-07-18 07:30:48
- OfStack
This article illustrates the XML file explanation class of php and its usage, which is a very practical skill. Share it for your reference. The details are as follows:
The XMLParser. class. php class files are as follows:
<?php
/** XML File analysis class
* Date: 2013-02-01
* Author: fdipzone
* Ver: 1.0
*
* func:
* loadXmlFile($xmlfile) Read in xml File output Array
* loadXmlString($xmlstring) Read in xmlstring Output Array
*/
class XMLParser{
/** Read xml Documents
* @param String $xmlfile
* @return Array
*/
public function loadXmlFile($xmlfile){
// get xmlfile content
$xmlstring = file_exists($xmlfile)? file_get_contents($xmlfile) : '';
// parser xml
list($flag, $data) = $this->parser($xmlstring);
return $this->response($flag, $data);
}
/** Read xmlstring
* @param String $xmlstring
* @return Array
*/
public function loadXmlString($xmlstring){
// parser xml
list($flag, $data) = $this->parser($xmlstring);
return $this->response($flag, $data);
}
/** Explanation xml Content
* @param String $xmlstring
* @return Array
*/
private function parser($xmlstring){
$flag = false;
$data = array();
// check xml format
if($this->checkXmlFormat($xmlstring)){
$flag = true;
// xml to object
$data = simpleXML_load_string($xmlstring, 'SimpleXMLElement', LIBXML_NOCDATA);
// object to array
$this->objectToArray($data);
}
return array($flag, $data);
}
/** Check xml Is the format correct
* @param String $xmlstring
* @return boolean
*/
private function checkXmlFormat($xmlstring){
if($xmlstring==''){
return false;
}
$xml_parser_obj = xml_parser_create();
if(xml_parse_into_struct($xml_parser_obj, $xmlstring, $vals, $indexs)===1){ // 1:success 0:fail
return true;
}else{
return false;
}
}
/** object Turn Array
* @param object $object
* @return Array
*/
private function objectToArray(&$object){
$object = (array)$object;
foreach($object as $key => $value){
if($value==''){
$object[$key] = "";
}else{
if(is_object($value) || is_array($value)){
$this->objectToArray($value);
$object[$key] = $value;
}
}
}
}
/** Output return
* @param boolean $flag true:false
* @param Array $data Transformed data
* @return Array
*/
private function response($flag=false, $data=array()){
return array($flag, $data);
}
}
?>
The Demo sample program is as follows:
<?php
require "XMLParser.class.php";
$xmlfile = 'file.xml';
$xmlstring = '<?xml version="1.0" encoding="utf-8"?>
<xmlroot>
<status>1000</status>
<info></info>
<result><id>100</id>
<name>fdipzone</name>
<gender>1</gender>
<age>28</age>
</result>
</xmlroot>';
echo '<pre>';
$xml_parser = new XMLParser();
echo "response xmlfile\r\n";
list($flag, $xmldata) = $xml_parser->loadXmlFile($xmlfile);
if($flag){
print_r($xmldata);
}
echo "response xmlstring\r\n";
list($flag, $xmldata) = $xml_parser->loadXmlString($xmlstring);
if($flag){
print_r($xmldata);
}
echo '</pre>';
?>
The XML predefined constants for PHP can be found in the official documentation:
http://www.php.net/manual/en/libxml.constants.php
I hope this article is helpful to everyone's study of PHP programming.