一个简单的xml类,可以解析和生成xml文件数据
2010-03-18 09:41
856 查看
<?php include('xml.php'); $data = XML_unserialize($xml); ?>
$xml即是xml文件的内容,$data是解析出的数组;
<?php include('xml.php'); $xml = XML_serialize($data); ?>
以上为使用实例,分别解析xml文档和生成xml格式的数据
xml.php源码
<?php ################################################################################### # # XML Library, by Keith Devens, version 1.2b # http://keithdevens.com/software/phpxml # # This code is Open Source, released under terms similar to the Artistic License. # Read the license at http://keithdevens.com/software/license # ################################################################################### ################################################################################### # 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 be 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 an attribute #we don't treat attributes by themselves, so for an empty element # that has 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's 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 be 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 will 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 are numeric #this is the third or 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 between 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; } ?>
相关文章推荐
- 一个解析XML数据的简单Demo(以KissXml为例)
- Android中pull解析和生成xml数据到一个字符串中
- box2d 描点工具生成b2Shape数据(xml通用格式),oc、c++解析模块类文件,b2Shape数据可视化工具
- 《JavaWeb---简单应用---练习JDBC,JSTL》---上传Excel文件,利用工具包解析,将数据储存到数据库中,可以查询删除
- 一个解析XML数据的简单Demo(以KissXml为例)
- 关于Dom4j解析XML文件,外带上一个简单工具
- Android中pull解析和生成xml数据到一个字符串中
- 一个可以生成复杂结构的json数据的简单例子
- Android_Xml 文件(数据) 生成 与 解析
- 一个模拟页面操作,解析xml输出,生成CSV文件的ruby程­序
- 35-002-1 struts前期之 通过JAVA反射机制及XML配置文件解析自定义一个MVC的简单框架
- box2d 描点工具生成b2Shape数据(xml通用格式),oc、c++解析模块类文件,b2Shape数据可视化工具
- 一个可以从excel中读取数据并生成xml的vba程序
- Java: 分析一个简单的配置文件并利用分析结果生成HTML文档
- java-----将目标XML文件数据解析为单一对象
- Ajax实现xml文件数据插入数据库(一)--- 构建解析xml文件的js库
- 从数据库得到数据导出指定格式的xml文件,上传到NC接口,返回回执到本地一个xml文件(接上篇补充)
- 一个生成文件固定格式的简单shell脚本
- Xml使用二三事: 小事2:做一个XML文件存储数据的知识整理工具UrlMgr。
- XML 生成一个XML文件