搜索
首页PHP 库Other librariesPHP数据格式与XML进行转换类
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

相关文章

LINQ 如何有效地将数据从宽格式转换为高格式以进行网格显示?LINQ 如何有效地将数据从宽格式转换为高格式以进行网格显示?

05Jan2025

使用 LINQ 透视数据在数据科学中,透视将数据从宽格式转换为高格式,反之亦然。假设您有一个数据集...

如何在 PHP 中将列式数据结构转换为基于行的格式?如何在 PHP 中将列式数据结构转换为基于行的格式?

25Oct2024

将多维列式数据重新排序为基于行的结构给定一个具有面向列数据的关联数组,任务是转置...

为什么我的 JNI 项目找不到依赖库?为什么我的 JNI 项目找不到依赖库?

21Nov2024

解决 UnsatisfiedLinkError: Can't Find Dependent Libraries in JNI Projects当开发 Java Native Interface (JNI) 项目时,您可能...

phpmyadmin不显示登录页面phpmyadmin不显示登录页面

30Oct2019

​phpmyadmin不显示登录页面解决方法:找到并打开phpMyAdmin\libraries\plugins\auth\AuthenticationCookie.class.php文件,将其中关于登录页面代码中的两处hide删除即可。

如何修复 phpMyAdmin 中的'count():参数必须是实现可数的数组或对象”错误?如何修复 phpMyAdmin 中的'count():参数必须是实现可数的数组或对象”错误?

20Oct2024

phpMyAdmin 错误:“count():参数必须是实现可数的数组或对象”您是否遇到过 phpMyAdmin 错误,指出“./libraries/sql.lib.php#601\ncount() 中的警告:参数必须是数组或对象

dacom.dll - 什么是 dacom.dll?dacom.dll - 什么是 dacom.dll?

19Oct2024

dacom.dll 在我的计算机上做什么? dacom.dll 是 Microsoft Corporation 创建的 Digital Anvil Component Libraries 使用的 DLL 文件。此过程仍在审核中。 dacom.dll 等非系统进程源自您安装的软件

See all articles