Example of parsing xml format data tool class in PHP, example of parsing xml tool class

Source: Internet
Author: User
Tags xml parser

Example of parsing xml format data tool class in PHP, example of parsing xml tool class

This example describes the PHP parsing xml format data tool class. We will share this with you for your reference. The details are as follows:

Class ome_xml {/*** xml resource ** @ var resource * @ see xml_parser_create () */public $ parser; /*** resource encoding *** @ 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 encodin G * @ 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 = f Ile_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-> pa Rser = xml_parser_create ($ this-> srcenc) {return 'unable to create XML parser resource '. $ this-> srcenc. 'encoding. ';}} if ($ this-> dstenc! = Null) {@ xml_parser_set_option ($ this-> parser, XML_OPTION_TARGET_ENCODING, $ this-> dstenc) or die ('invalidtarget encoding ');} xml_parser_set_option ($ this-> parser, XML_OPTION_CASE_FOLDING, 0); // lowercase tags xml_parser_set_option ($ this-> parser, delimiter, 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 ($ this-> 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] ['bubuckets'])? $ This-> _ struct [$ I] ['bubuckets']: '', $ this-> getChild ($ I); unset ($ this-> _ struct ); return $ tree ;} /*** recursion the children node data ** @ access public * @ param integer [$ 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 ($ t His-> _ struct [$ I] ['value'])? $ This-> _ struct [$ I] ['value']: ''; $ attributes = isset ($ this-> _ struct [$ I] ['bubuckets'])? $ This-> _ struct [$ I] ['bubuckets']: ''; switch ($ this-> _ struct [$ I] ['type']) {case 'open': // node has more children $ child = $ this-> getChild ($ I ); // append the children data to the current node $ children = $ this-> addNode ($ children, $ tagname, $ value, $ attributes, $ child); break; case 'complete': // at end of current branch $ children = $ this-> addNode ($ children, $ tagname, $ value, $ attributes); break; Case 'cdata': // node has 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 an array ** @ access public * @ param array [$ target] * @ param string [$ key] * @ param string [$ value] * @ param array [$ attributes] * @ param array [$ inner] the children * @ r Eturn 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 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-> parser) & is_resource ($ this-> parser) {xml_parser_free ($ this-> parser); unset ($ this-> parser );}}}

PS: Here are some online tools for xml operations for your reference:

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

Online formatting XML/online compression XML:
Http://tools.jb51.net/code/xmlformat

XMLOnline compression/formatting tools:
Http://tools.jb51.net/code/xml_format_compress

XMLCode Online formatting and beautification tools:
Http://tools.jb51.net/code/xmlcodeformat

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.