PHP Class: XML File Parsing class

Source: Internet
Author: User

XMLParser.class.php

<?php/** XML File Analysis class * date:2013-02-01 * author:fdipzone * ver:1.0 * Func: * Loadxmlfile ($x  Mlfile) read into XML file output array * loadxmlstring ($xmlstring) read into 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 * @return Array/Public Function lo  
      
        Adxmlstring ($xmlstring) {//Parser XML list ($flag, $data) = $this->parser ($xmlstring);  
      
    return $this->response ($flag, $data);   /** interpret XML content * @paramString $xmlstring * @return Array/Private Function parser ($xmlstring) {$flag = 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 ($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 * See more highlights in this column: Http://www.bianceng.  
              
        cnhttp://www.bianceng.cn/webkf/php/* @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 array/Private Function response ($flag =false, $data =array ()) {  
          
    Return Array ($flag, $data);  
     } 
}?> 

Demo:

<?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> ';  
? >

PHP XML Predefined constants: http://www.php.net/manual/en/libxml.constants.php

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.