PHP parsing XML Format Data Tool Class example explained

Source: Internet
Author: User
Tags cdata xml parser
This article mainly introduces the PHP parsing XML Format Data tool class, involving PHP for XML Format Data node addition, acquisition, parsing and other related operational skills, the need for friends can refer to the following

The examples in this article describe the PHP parsing XML Format Data tool class. Share to everyone for your reference, as follows:

Class Ome_xml {/** * XML resource * * @var resource * @see xml_parser_create () */public $parser;  /** * Resource Code * * @var String */public $srcenc;  /** * Target Encoding * * @var String */public $dstenc;  /** * The original struct * * @access private * @var array */Public $_struct = Array ();  /** * Constructor * * @access public * @param mixed [$SRCENC] source encoding * @param mixed [$dstenc] Target encoding * @return void * @since * * Function sofeexmlparser ($srcenc = null, $dstenc = null) {$this    Srcenc = $srcenc;    $this->dstenc = $dstenc;    Initialize the variable.    $this->parser = null;  $this->_struct = Array ();  }/** * Parses the XML file * * @access public * @param string [$file] the XML file name * @return void  * @since */function Xml2array ($file) {//$this->sofeexmlparser (' utf-8 ');    $data = file_get_contents ($file);    $this->parsestring ($data); Return $this->gettree ();  } function Xml3array ($file) {$data = file_get_contents ($file);  $this->parsestring ($data);  return $this->_struct;  }/** * Parses a string. * * @access Public * @param string data XML data * @return void */function parsestring ($data) {if ($    This->srcenc = = = null) {$this->parser = Xml_parser_create (); } else {if ($this->parser = xml_parser_create ($this->srcenc)) {return ' Unable to create XML parser Reso Urce with '.      $this->srcenc. ' Encoding. '; }} if ($this->dstenc!== null) {@xml_parser_set_option ($this->parser, xml_option_target_encoding, $this    ->dstenc) or Die (' Invalid target encoding ');  } xml_parser_set_option ($this->parser, xml_option_case_folding, 0);    Lowercase tags xml_parser_set_option ($this->parser, Xml_option_skip_white, 1); Skip empty tags if (!xml_parse_into_struct ($this->parser, $data, $this->_struct)) {/*printf ("XML error:%s at line%d ", Xml_error_string (Xml_get_error_code ($this->parser)), Xml_get_current_line_number ($thi      S->parser)); * * $this->free ();    return false;    } $this->_count = count ($this->_struct);  $this->free ();    }/** * Return the data struction * * @access public * @return Array */function Gettree () {$i = 0;    $tree = Array (); $tree = $this->addnode ($tree, $this->_struct[$i] [' tag '], (isset ($this->_struct[$i] [' value ']))? $ this->_struct[$i [' Value ']: ', (Isset ($this->_struct[$i [' attributes ']))?    $this->_struct[$i] [' Attributes ']: ", $this->getchild ($i));    unset ($this->_struct);  return $tree; }/** * Recursion the Children node data * * @access public * @param integers [$i] The last struct index * @    return array */function Getchild (& $i) {//contain node data $children = Array (); Loop while (+ + $i < $this->_coUNT) {//node tag name $tagname = $this->_struct[$i] [' tag ']; $value = Isset ($this->_struct[$i [' value '])?      $this->_struct[$i [' Value ']: '; $attributes = Isset ($this->_struct[$i] [' attributes '])?      $this->_struct[$i [' Attributes ']: '; Switch ($this->_struct[$i [' type ']) {case ' open '://node have more children $child = $this-&G          T;getchild ($i); Append the children data to the current node $children = $this->addnode ($children, $tagname, $value, $attri          Butes, $child);        Break Case "complete"://At end of current Branch $children = $this->addnode ($children, $tagname, $value,          $attributes);        Break          Case ' CDATA '://node have CDATA after one of it ' s children $children [' value ']. = $value;        Break          Case ' close '://end of node, return collected data return $children;      Break }}//return $children; }/** * Appends some values to a array * * @access public * @param array [$target] * @param string [ $key] * @param string [$value] * @param array [$attributes] * @param array [$inner] the children * @ return void * @since */function AddNode ($target, $key, $value = ", $attributes =", $child = ") {if (!isset (      $target [$key] [' value ']) &&!isset ($target [$key][0]) {if ($child! = ") {$target [$key] = $child;        if ($attributes! = ") {foreach ($attributes as $k = = $v) {$target [$key] [$k] = $v;    }} $target [$key] [' value '] = $value;        } else {if (!isset ($target [$key][0])) {//is a string or other $oldvalue = $target [$key];        $target [$key] = array ();        $target [$key][0] = $oldvalue;      $index = 1;      } else {//is array $index = count ($target [$key]); if ($child! = ") {$target [$key] [$index] = $child;        if ($attributes! = ") {foreach ($attributes as $k = = $v) {$target [$key] [$index] [$k] = $v;    }} $target [$key] [$index] [' value '] = $value;  } return $target; }/** * Free the resources * * @access public * @return void **/function free () {if (Isset ($this->par      Ser) && Is_resource ($this->parser)) {Xml_parser_free ($this->parser);    unset ($this->parser); }  }}

PS: Here are a few more online tools for you to refer to using XML operations:

Online XML/json Mutual Conversion Tool:
Http://tools.jb51.net/code/xmljson

Online formatting of XML/Online compressed XML:
Http://tools.jb51.net/code/xmlformat

XML Online Compression/formatting tools:
Http://tools.jb51.net/code/xml_format_compress

XML Code online format Beautification tool:
Http://tools.jb51.net/code/xmlcodeformat

Articles you may be interested in:

The difference between class static calls and scope resolution operators in PHP

PHP array-based implementation of the stack and queue function example detailed

A detailed analysis of error handling and exception handling methods based on PHP7

Related Article

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.