This paper describes the XML operation classes implemented by PHP. Share to everyone for your reference, as follows:
This is an interface program that requires a large number of analytic parsing xml,php xml_parse_into_struct () function can not directly generate an easy-to-use array, and simplexml extension is supported in PHP5, so browse the search engine, Found a good PHP XML operation class on the Foreigner's website.
I. Examples of usage:
1. Interpret the XML file as an easy-to-use array:
<?phpinclude (' xml.php '); Reference PHP XML operation class $xml = file_get_contents (' Data.xml '); Read XML file//$xml = file_get_contents ("Php://input"); Read the input stream $data=xml_unserialize ($xml) from the post, Echo ' <pre> ';p rint_r ($data), echo ' </pre> ';? >
Data.xml file:
<?xml version= "1.0" encoding= "GBK"? ><video><upload><videoid>998</videoid><name ><! [Cdata[Memories of the future]]></name><memo><! [cdata[def]]></memo><up_userid>11317</up_userid></upload></video>
The corresponding array generated by the XML operation class (Chinese character coding: UTF-8):
Array ([VIDEO] = = Array ([ upload] = = Array ( [videoid] = 998 [name] = memory Future [Memo] ] = def [Up_userid] = 11317 )))
2. Convert an array to an XML file:
<?phpinclude (' xml.php ');//reference PHP XML operation class $xml = Xml_serialize ($data); >
Second, the PHP XML operation class Source code:
<?php#################################################################################### XML_unserialize: Takes raw XML as a parameter (a string) # and returns an equivalent PHP data structure##################################### ############################################# #function & Xml_unserialize (& $xml) {$xml _parser = &new XML ( ); $data = & $xml _parser->parse ($xml); $xml _parser->destruct (); return $data;} #################################################################################### xml_serialize:serializes Any PHP data structure into xml# Takes one parameter:the data to serialize. Must is an array.################################################################################## #function & Xml_serialize (& $data, $level = 0, $prior _key = NULL) {if ($level = = 0) {Ob_start (); Echo ' <?xml version= ' 1.0 '?> ' , "\ n"; } while (List ($key, $value) = each ($data)) if (!strpos ($key, ' attr ')) #if It's not a attribute #we don ' t treat Attribut Es by ThemsElves, so for a emptyempty element # that have attributes you still need to set the element to NULL if (Is_array ($value ) and array_key_exists (0, $value)) {xml_serialize ($value, $level, $key); }else{$tag = $prior _key? $prior _key: $key; echo str_repeat ("\ t", $level), ' < ', $tag; if (array_key_exists ("$key attr", $data)) {#if There ' an attribute for this element while (list ($attr _name, $attr _value ) = each ($data ["$key attr"])) echo ', $attr _name, ' = ', Htmlspecialchars ($attr _value), ' "; Reset ($data ["$key attr"]); } if (Is_null ($value)) echo "/>\n"; ElseIf (!is_array ($value)) echo ' > ', Htmlspecialchars ($value), "</$tag >\n"; else echo ">\n", Xml_serialize ($value, $level + 1), str_repeat ("\ t", $level), "</$tag >\n"; } reset ($data); if ($level = = 0) {$str = &ob_get_contents (); Ob_end_clean (); return $str;}} #################################################################################### XML class:utility class to Bes Used with PHP' s XML handling functions################################################################################## #class xml{var $parser; #a reference to the XML parser var $document, #the entire XML structure built up so far var $parent; #a Pointer to the current parent-the parent would be an array var $stack; #a stack of the most recent parent at each nesting level var $last _opened_tag; #keeps track of the last tag opened. function XML () {$this->parser = &xml_parser_create (); Xml_parser_set_option (& $this->parser, xml_option_case_folding, false); Xml_set_object (& $this->parser, & $this); Xml_set_element_handler (& $this->parser, ' open ', ' close '); Xml_set_character_data_handler (& $this->parser, ' data '); } function Destruct () {Xml_parser_free (& $this->parser),} function & Parse (& $data) {$this->document = Array (); $this->stack = Array (); $this->parent = & $this->document; Return Xml_parse (& $this->parser, & $datA, true)? $this->document:null; } function open (& $parser, $tag, $attributes) {$this->data = '; #stores Temporary CDATA $this->last_opened_tag = $tag; if (Is_array ($this->parent) and Array_key_exists ($tag, $this->parent)) {#if You ' ve seen this tag before if (Is_array ($this->parent[$tag]) and array_key_exists (0, $this->parent[$tag])) {#if The keys is numeric #this is the third O R later instance of $tag we ' ve come across $key = Count_numeric_items ($this->parent[$tag]); }else{#this is the second instance of $tag that we ve seen. Shift Around if (array_key_exists ("$tag attr", $this- Parent) {$arr = Array (' 0 attr ' =>& $this->parent["$tag attr"], & $this->parent[$tag]); unset ($this->parent["$tag attr"]); }else{$arr = Array (& $this->parent[$tag]); } $this->parent[$tag] = & $arr; $key = 1; } $this->parent = & $this->parent[$tag]; }else{$key = $tag; } if ($attributes) $this->parent["$key attr"] = $attributes; $this->parent = & $this->parent[$key]; $this->stack[] = & $this->parent; } function data (& $parser, $data) {if ($this->last_opened_tag! = NULL) #you don ' t need to store whitespace in Betwee N tags $this->data. = $data; } function Close (& $parser, $tag) {if ($this->last_opened_tag = = $tag) {$this->parent = $this->data; $this->last_opened_tag = NULL; } array_pop ($this->stack); if ($this->stack) $this->parent = & $this->stack[count ($this->stack)-1]; }}function count_numeric_items (& $array) {return Is_array ($array)? Count (Array_filter (Array_keys ($array), ' Is_ Numeric ')): 0;}? >
I hope this article is helpful to you in PHP programming.