This article describes the php XML file interpretation class and its usage. it is a very practical technique. Share it with you for your reference. The XMLParser. class. php class file is as follows :? Php ** XML file analysis class * Date: 2013-02-01 * Author: fdipzone * This article describes the XML file interpretation class of php and its usage, which is a very practical technique. Share it with you for your reference. The details are as follows:
The XMLParser. class. php class file is as follows:
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) {$ flag = false; $ data = array (); // chec K 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 whether the xml format is correct * @ param String $ xmlstring * @ return boolean */private function checkXmlFormat ($ xmlstring) {if ($ xmlstring = '') {return false;} $ xml_parser_o Bj = xml_parser_create (); if (xml_parse_ct _struct ($ xml_parser_obj, $ xmlstring, $ vals, $ indexs) === 1) {// 1: success 0: fail return true ;} else {return false ;}}/** convert 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_arra Y ($ value) {$ this-> objectToArray ($ value); $ object [$ key] = $ value ;}}}} /** output return * @ param boolean $ flag true: false * @ param Array $ data converted data * @ return Array */private function response ($ flag = false, $ data = array () {return array ($ flag, $ data) ;}}?>
The Demo sample program is as follows:
1000
100
fdipzone
1
28
'; echo ''; $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 ''; ?>
For XML pre-defined constants in PHP, refer to the official documentation:
Http://www.php.net/manual/en/libxml.constants.php
I hope this article will help you learn PHP programming.