PHP XML file Read XMLReader

Source: Internet
Author: User

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

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.