代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
PHP
】
XML与数据格式进行转换类
作者:
qita.in
/ 发布于
2012/10/22
/
534
XML与数据格式进行转换类
<div><?php</div> <div>/**</div> <div>* xml2array() will convert the given XML text to an array in the XML structure.</div> <div>* Link: http://www.bin-co.com/php/scripts/xml2array/</div> <div>* Arguments : $contents - The XML text</div> <div>* $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.</div> <div>* $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.</div> <div>* Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.</div> <div>* Examples: $array = xml2array(file_get_contents('feed.xml'));</div> <div>* $array = xml2array(file_get_contents('feed.xml', 1, 'attribute'));</div> <div>*/</div> <div>function xml2array($contents, $get_attributes = 1, $priority = 'tag') {</div> <div> if (!$contents) return array();</div> <div></div> <div> if (!function_exists('xml_parser_create')) {</div> <div> // print "'xml_parser_create()' function not found!";</div> <div> return array();</div> <div> } <div> // Get the XML parser of PHP - PHP must have this module for the parser to work</div> <div> $parser = xml_parser_create('');</div> <div> xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss</div> <div> xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);</div> <div> xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);</div> <div> xml_parse_into_struct($parser, trim($contents), $xml_values);</div> <div> xml_parser_free($parser);</div> <div></div> <div> if (!$xml_values) return; //Hmm... </div> <div> // Initializations</div> <div> $xml_array = array();</div> <div> $parents = array();</div> <div> $opened_tags = array();</div> <div> $arr = array();</div> <div></div> <div> $current = &$xml_array; //Refference </div> <div> // Go through the tags.</div> <div> $repeated_tag_index = array(); //Multiple tags with same name will be turned into an array</div> <div> foreach($xml_values as $data) {</div> <div> unset($attributes, $value); //Remove existing values, or there will be trouble </div> <div> // This command will extract these variables into the foreach scope</div> <div> // tag(string), type(string), level(int), attributes(array).</div> <div> extract($data); //We could use the array by itself, but this cooler.</div> <div> $result = array();</div> <div> $attributes_data = array();</div> <div></div> <div> if (isset($value)) {</div> <div> if ($priority == 'tag') $result = $value;</div> <div> else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode</div> <div> } <div> // Set the attributes too.</div> <div> if (isset($attributes) and $get_attributes) {</div> <div> foreach($attributes as $attr => $val) {</div> <div> if ($priority == 'tag') $attributes_data[$attr] = $val;</div> <div> else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'</div> <div> } <div> } <div> // See tag status and do the needed.</div> <div> if ($type == "open") { // The starting of the tag '<tag>'</div> <div> $parent[$level-1] = &$current;</div> <div> if (!is_array($current) or (!in_array($tag, array_keys($current)))) { // Insert New tag</div> <div> $current[$tag] = $result;</div> <div> if ($attributes_data) $current[$tag . '_attr'] = $attributes_data;</div> <div> $repeated_tag_index[$tag . '_' . $level] = 1;</div> <div></div> <div> $current = &$current[$tag];</div> <div> } else { // There was another element with the same tag name</div> <div> if (isset($current[$tag][0])) { // If there is a 0th element it is already an array</div> <div> $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;</div> <div> $repeated_tag_index[$tag . '_' . $level]++;</div> <div> } else { // This section will make the value an array if multiple tags with the same name appear together</div> <div> $current[$tag] = array($current[$tag], $result); //This will combine the existing item and the new item together to make an array</div> <div> $repeated_tag_index[$tag . '_' . $level] = 2;</div> <div></div> <div> if (isset($current[$tag . '_attr'])) { // The attribute of the last(0th) tag must be moved as well</div> <div> $current[$tag]['0_attr'] = $current[$tag . '_attr'];</div> <div> unset($current[$tag . '_attr']);</div> <div> } <div> } <div> $last_item_index = $repeated_tag_index[$tag . '_' . $level]-1;</div> <div> $current = &$current[$tag][$last_item_index];</div> <div> } <div> } elseif ($type == "complete") { // Tags that ends in 1 line '<tag />'</div> <div> // See if the key is already taken.</div> <div> if (!isset($current[$tag])) { // New Key</div> <div> $current[$tag] = $result;</div> <div> $repeated_tag_index[$tag . '_' . $level] = 1;</div> <div> if ($priority == 'tag' and $attributes_data) $current[$tag . '_attr'] = $attributes_data;</div> <div> } else { // If taken, put all things inside a list(array)</div> <div> if (isset($current[$tag][0]) and is_array($current[$tag])) { // If it is already an array...</div> <div> // ...push the new element into that array.</div> <div> $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;</div> <div></div> <div> if ($priority == 'tag' and $get_attributes and $attributes_data) {</div> <div> $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;</div> <div> } <div> $repeated_tag_index[$tag . '_' . $level]++;</div> <div> } else { // If it is not an array...</div> <div> $current[$tag] = array($current[$tag], $result); //...Make it an array using using the existing value and the new value</div> <div> $repeated_tag_index[$tag . '_' . $level] = 1;</div> <div> if ($priority == 'tag' and $get_attributes) {</div> <div> if (isset($current[$tag . '_attr'])) { // The attribute of the last(0th) tag must be moved as well</div> <div> $current[$tag]['0_attr'] = $current[$tag . '_attr'];</div> <div> unset($current[$tag . '_attr']);</div> <div> } <div></div> <div> if ($attributes_data) {</div> <div> $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;</div> <div> } <div> } <div> $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken</div> <div> } <div> } <div> } elseif ($type == 'close') { // End of tag '</tag>'</div> <div> $current = &$parent[$level-1];</div> <div> } <div> } <div></div> <div> return($xml_array);</div> <div>} <div></div> <div>// Array to XML</div> <div>class array2xml {</div> <div> public $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";</div> <div> public $sub_item = array();</div> <div> public function __construct($array) {</div> <div> $sub_item = array();</div> <div> $this->output .= $this->xmlmake($array);</div> <div> } <div> public function xmlmake($array, $fk = '') {</div> <div> $xml = '';</div> <div> global $sub_item;</div> <div> foreach ($array as $key => $value) {</div> <div> if (is_array($value)) {</div> <div> if (is_numeric($key)) {</div> <div> $this->sub_item=array_merge($this->sub_item,array($fk));</div> <div> $xml .= "<{$fk}>" . $this->xmlmake($value, $key) . "</{$fk}>";</div> <div> } else {</div> <div> $xml .= "<{$key}>" . $this->xmlmake($value, $key) . "</{$key}>";</div> <div> } <div> } else {</div> <div> $xml .= "<{$key}>{$value}</{$key}>\n";</div> <div> } <div> } <div> return $xml;</div> <div> } <div> public function output(){</div> <div> foreach($this->sub_item as $t){</div> <div> $this->output = str_replace("<{$t}><{$t}>","<{$t}>",$this->output);</div> <div> $this->output = str_replace("</{$t}></{$t}>","</{$t}>",$this->output);</div> <div> } <div> return $this->output;</div> <div> } <div>}
试试其它关键字
XML与数据格式
同语言下
.
用net匹配并替换iOS标准的emoji表情符号
.
处理带Emoji表情的的字符串
.
获取微信昵称时 过滤特殊字符
.
通过判断上传文件的头字符来判断文件的类型
.
模拟百度URL加密解密算法
.
以太坊检查地址是否合法
.
实现crontab解析类
.
获取每个月的开始和结束时间
.
图片上传工具类
.
APP手机应用信息采集
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
qita.in
贡献的其它代码
(
28
)
.
跨站刷票代码
.
计算一个文件夹的大小
.
全屏点弹代码
.
替代标题
.
实现字符串翻转(包含中文汉字)
.
PHP中英文断句无乱码
.
各种过滤字符函数
.
获取当前页面完整URL地址
.
将html 转成wml WAP标记语言
.
检查用户名是否符合规定
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3