PHP XML File read
<?PHP/** $xmlString = ' <xml> <persons count= ' > <person username= ' username1 ' age= ' >th IS is username1 description</person> <person username= "username2" age= "a" >this is username2 DESCRI ption</person> </persons> </xml> '; $mXmlTest = new XmlTest (), $dataList = $mXmlTest->parsexmlstr ($xmlString);p rint_r ($dataList); $xmlFile = './test.xml '; $dataList = $mXmlTest->parsexmlfile ($xmlFile);p rint_r ($dataList); */classxmltest{//<xml>//<persons count= "Ten" >//<person username= "username1" age= "a" >this I s username1 description</person>//<person username= "username2" age= "a" >this is username2 descrip tion</person>//</persons>//</xml> /** * Parse the returned XML document*/ Public functionParsexmlfile ($xmlFile){ $reader=New\xmlreader (); $reader->open ($xmlFile, ' UTF-8 '); $nodeName= ' '; $dataList=Array(); $data=Array(); while($reader-Read ()) { if($reader->nodetype = = \xmlreader::ELEMENT) { $nodeName=$reader-name; if($nodeName= = ' persons '){ $count=$reader->getattribute (' Count '); if(! ($count>0)){ Break; } } ElseIf($nodeName= = ' Person '){ $data=Array( ' Username ' =$reader->getattribute (' username '), ' age ' =$reader->getattribute (' age '), ); } } if($reader->nodetype = = \xmlreader::text &&!Empty($nodeName)){ if($nodeName= = ' Person '){ $data[' description '] =Strtolower($reader-value); $dataList[] =$data; } } } $reader-Close (); return $dataList; } /** * Parse the returned XML document*/ Public functionPARSEXMLSTR ($xmlString){ $reader=New\xmlreader (); $reader->xml ($xmlString, ' UTF-8 '); $nodeName= ' '; $dataList=Array(); $data=Array(); while($reader-Read ()) { if($reader->nodetype = = \xmlreader::ELEMENT) { $nodeName=$reader-name; if($nodeName= = ' persons '){ $count=$reader->getattribute (' Count '); if(! ($count>0)){ Break; } } ElseIf($nodeName= = ' Person '){ $data=Array( ' Username ' =$reader->getattribute (' username '), ' age ' =$reader->getattribute (' age '), ); } } if($reader->nodetype = = \xmlreader::text &&!Empty($nodeName)){ if($nodeName= = ' Person '){ $data[' description '] =Strtolower($reader-value); $dataList[] =$data; } } } return $dataList; }}
PHP XML file Read XMLReader