PHP数据格式与XML进行转换类
<?php function xml2array($contents, $get_attributes = 1, $priority = 'tag') { if (!$contents) return array(); if (!function_exists('xml_parser_create')) { // print "'xml_parser_create()' function not found!"; return array(); } // Get the XML parser of PHP - PHP must have this module for the parser to work $parser = xml_parser_create(''); 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 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, trim($contents), $xml_values); xml_parser_free($parser); if (!$xml_values) return; //Hmm... // Initializations $xml_array = array(); $parents = array(); $opened_tags = array(); $arr = array(); $current = &$xml_array; //Refference // Go through the tags. $repeated_tag_index = array(); //Multiple tags with same name will be turned into an array foreach($xml_values as $data) { unset($attributes, $value); //Remove existing values, or there will be trouble // This command will extract these variables into the foreach scope // tag(string), type(string), level(int), attributes(array). extract($data); //We could use the array by itself, but this cooler. $result = array(); $attributes_data = array(); if (isset($value)) { if ($priority == 'tag') $result = $value; else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode }
这是一个可以在XML和数据格式中互相转换的类库,需要的朋友可以下载使用。
免责声明
本站所有资源均由网友贡献或各大下载网站转载。请自行检查软件的完整性!本站所有资源仅供学习参考。请不要将它们用于商业目的。否则,一切后果由您负责!如有侵权,请联系我们删除。联系方式:admin@php.cn
相关文章
Oracle 数据库现在支持布尔数据类型吗?
12 Jan 2025
Oracle 数据库中布尔数据类型的可用性与其他 RDBMS(例如提供 BIT 数据类型的 Microsoft SQL Server)不同,Oracle 数据库...
在 Joomla 中连接第三方 PHP 库
28 Dec 2024
PHP 库很方便,因为可以从应用程序中的任何位置访问它们:从插件、组件模型、模块等。如果有人已经解决了类似的问题并将其设计为库(甚至更新了它),那就有意义了
Hot Tools
热门文章
崩坏:星穹铁道 - 所有金色替罪羊谜题解决方案
18 Jan 2025
手游攻略
Tales Of Graces F 重制版:所有锁定的宝箱密码
18 Jan 2025
手游攻略
鲁马岛:考古学家职业指南
03 Jan 2025
手游攻略
如何修复 KB5049622 在 Windows 11 中安装失败的问题?
15 Jan 2025
故障排查
鲁玛岛:所有鲁玛蛋地点
05 Jan 2025
手游攻略