PHP XML File Interpretation class application example

Source: Internet
Author: User
Tags php class

The XMLParser.class.php class files are as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98-99 100 <?php /** XML File Analysis class   *  date:  2013-02-01  *  author:fdipzone  *  ver:  1.0  *  *  func:  *  loadxmlfile ($xmlfile)    read XML file Output array  Loadxmlstring ($xmlstring) Read xmlstring output array  */      class xmlparser{         /** read XML file     * @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 &nbsp   * @return array    */    public function loadxmlstring ($xmlstring) {  & nbsp;   //parser xml      list ($flag, $data) = $this->parser ($xmlstring) ;      return $this->response ($flag, $data);    }         /** Interpreting XML content     * @param   String $xmlstring     * @return Array     */    private function parser ($xmlstring) {       $flag = false;       $data = Array ();      //check XML format   & Nbsp;  if ($this->checkxmlformat ($xmlstring)) {         $flag = True ;        //XML to object         $data = Simplexml_load_string ($xmlstring, ' simplexmlelement ', libxml_nocdata);  &Nbsp;     //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 ($ 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 to array    * @param object $object     * @r Eturn 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 converted data     * @return Ar ray    */    private function response ($flag =false, $data =array ()) {       return Array ($flag, $data);    } } ?>

The

Demo sample program is as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

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.