Describes how to use a php xml class.

Source: Internet
Author: User
Tags xml parser
Today, a program that re-writes an interface in the PHP4 environment needs to parse XML in a large amount. The xml_parse_into_struct () function of PHP cannot directly generate an easy-to-use array.

Preface

Today, a program that re-writes an interface in the PHP4 environment needs to parse XML in a large amount. The xml_parse_into_struct () function of PHP cannot directly generate an easy-to-use array. XML operation class. ========================================================== =====================================1, interpret the XML file as an easy-to-use array: <? PHP includes ('XML. php ') // reference the php xml operation class $ XML = obtain the webpage ("data. "); // read the xml file // $ XML = obtain the webpage (" php: // input supported ") // read the input stream from POST $ Data = XML_unserialize (XML); Echo'
'; Print_r (metadata); Echo"
 ';?> ######################################## ######################### Data. xml File: <? XML version = "1.0" encoding = "GBK"?>
 
  
Of
  <上载>

   
    
998
    <名称>
     
<! [CDATA [recalling the future]> Chinese
     
      
<! [CDATA [HD]>
      
        11317
       ######################################## ########################## Use the XML operation class to generate the corresponding array (Chinese character encoding: UTF-8 ): arrange ([video] => arrays ([Upload] => arrays ([Video ID] => 998 [name] => Future Memories [memo] => DEF [up_userid] => 11317 ))) ========================================================== ======================================= 2, convert an array to an XML file: <? PHP includes ('XML. php'); // reference the xml operation class of php $ XML = XML_serialize (metadata);?> ========================================================== =====================================3, php xml Operation Source code <? PHP ####################################### ######################################## ###### XML Library, this code is open-source and is released under similar terms of Artistic License. # Read permission ##################################### ######################################## ######################################## ######################################## ########## XML_unserialize: use the original XML as a parameter (string) # return an equivalent PHP Data structure ############################### ######################################## ########### function XML_unserialize (XML) {$ xml_parser = new XML (); $ Data = & $ xml_parser> parsing ($ XML); $ xml_par Ser> self-destruction (); returned data ;} ######################################## ######################################## #### XML_serialize: any PHP Data structure is serialized to XML # with a parameter: data sequence. It must be an array. ######################################## ######################################## ### Function XML_serialize (metadata, meta level = 0, $ prior_key = NULL) {if ($ horizontal = 0) {ob_start (); Echo '<? XML version = "1.0"?> "," \ N ";} (list ($ key, value) = (data) (starting position of the string, strpos ($ key 'attr )) # If it is not an attribute # We do not treat our own attributes, so for the emptyempty element # has attributes, you still need to set the element to NULL (IS_ARRAY (value) and array_key_exists (0, $ value) {XML_serialize (key value $ level, dollar) ;}{ tag $ = $ prior_key? $ Prior_key: Dollar key; Echo str_repeat ("\ t", $ level), "<", $ tag; if (array_key_exists ("$ ATTR key", metadata )) {# If attributes of this element exist and (list ($ attr_name, attribute value) = ($ Data ["$ key ATTR"]) Echo ', $ attr_name, "=" ', using htmlspecialchars ($ attribute value),' "; reset ($ Data [" $ ATTR key "]);} (IS_NULL (value )) echo "/> \ N"; elseif (! IS_ARRAY (value) Echo '>', using htmlspecialchars (value),"\ N "; other Echo"> \ ", XML_serialize (value $ level + 1), str_repeat (" \ t ", $ level),"\ N ";} Reset (metadata); (dollar level = 0) {$ Strait = ob_get_contents (); ob_end_clean (); $ Strait returned ;}} ######################################## ######################################## #### XML class: practical tools to use PHP's XML processing functions ############################### ######################################## ########### XML class {variable $ parser XML parser reference variable $ file; # The entire XML structure is established, so the variable $ # The current parent of the parent pointer-the parent will be an array variable $ stack # The most recent parent of the stack at each nested level var directory $ last_opened_tag; # The Last tag opened by the trail. Function XML () {-> parser = corresponds to xml_parser_create (); xml_parser_set_option (& $> parser, XML_OPTION_CASE_FOLDING, false); xml_set_object (& $> parser, $); xml_set_element_handler (& $-> parser, 'open', 'near'); xml_set_character_data_handler (& $-> parser, 'data ');} self-destruction function () {xml_parser_free (& $-> parser);} function parsing (data) {-> File = array ();-> stack = array (); -> parent = & $-> data; returns xml_parse once (& $-> parser, & $ data, real )? $-> File: NULL;} feature enabled (& $ parser, $ tag $ attribute) {-> data = ''; # store temporary CDATA-> last_opened_tag = $ tag; (IS_ARRAY (parent) and array_key_exists ($ tag-> parent) {# If you see this tag (IS_ARRAY ($> parent [tag]) and array_key_exists (0, $-> parent [tag]) {# if the key is a number # This is the third or later $ tag instance, we have encountered the second instance marked with the dollar key = count_numeric_items ($-> parent [tag]) ;}{# $, which we have seen. If you want to migrate (array_key_exists ("$ tag attribute", $-> parent company )) {arrival = array ('0 attribute' =>&$-> parent ["$ tag ATTR"], & $-> parent [tag]); not set ($-> parent ["$ tag ATTR"]) ;}{ arrival = array (& $-> parent [tag]);} $> parent [tag] = & $ arrival; key of USD = 1 ;}$> Parent Company =&$-> parent [tag] ;}{$ = $ tag ;} (attribute)-> parent "key feature"] = $ attribute;-> parent = & $-> parent [key]; [] =$> stacked & $-> parent;} function data (& $ analyzer, data) {($> last_opened_tag! = NULL) # you do not need to store the blank space between tags-> data = $ Data;} function is disabled (& $ parser, $ tag) {if ($ last_opened_tag ==$ tag) {$> Parent Company =$-> data;-> last_opened_tag = NULL;} array_pop ($ stack); (this stack) $> Parent Company =&> stackable count ($> stackable)-1] ;}} function count_numeric_items (& $ Array) {returns IS_ARRAY (array )? Count (array_filter (get array_keys ($ Array), is_numeric): 0 ;}?>
      
     
    
   
  
 

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.