首页 PHP 库 其它类库 PHP数据格式与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 如何有效地将数据从宽格式转换为高格式以进行网格显示?

05 Jan 2025

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

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

25 Oct 2024

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

如何使用自连接从自引用表中检索数据? 如何使用自连接从自引用表中检索数据?

13 Jan 2025

了解 SELF JOIN 及其应用程序在数据库管理领域中,SELF JOIN 是一种特殊类型的联接操作,它允许...

Oracle 数据库现在支持布尔数据类型吗? Oracle 数据库现在支持布尔数据类型吗?

12 Jan 2025

Oracle 数据库中布尔数据类型的可用性与其他 RDBMS(例如提供 BIT 数据类型的 Microsoft SQL Server)不同,Oracle 数据库...

在 Joomla 中连接第三方 PHP 库 在 Joomla 中连接第三方 PHP 库

28 Dec 2024

PHP 库很方便,因为可以从应用程序中的任何位置访问它们:从插件、组件模型、模块等。如果有人已经解决了类似的问题并将其设计为库(甚至更新了它),那就有意义了

为什么要从 Python 中的'object”继承:一个类继承问题 为什么要从 Python 中的'object”继承:一个类继承问题

24 Dec 2024

了解 Python 类继承在 Python 中,类可以从其他类继承,这使它们能够访问父类的属性并...

See all articles