This article illustrates the PHP XML file interpretation class and its usage, which is a very practical technique. Share to everyone for your reference. Specifically 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 into xmlstring output array/class xmlparser{/** read XML file * @param Str ing $xmlfile * @return Array */Public Function Loadxmlfile ($xmlfile) {//Get xmlfile content $xmlstrin g = 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); /** interpret XML content * @param String $xmlstring * @return Array/Private Function parser ($xmlstring) {$f
lag = 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 that the XML format is correct * @param String $xmlstring * @return Boolean/Private function Checkxmlformat ($xmlstr
ing) {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 to 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 Returns * @param boolean $flag true:false * @param Array $data converted data * @return ARR
AY */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> ';
? >
XML predefined constants for PHP refer to the official documentation:
http://www.php.net/manual/en/libxml.constants.php
I hope this article will help you with the learning of PHP programming.