首頁 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

將多維列式資料重新排序為基於行的結構給定一個具有面向列資料的關聯數組,任務是轉置...

為什麼要從 Python 中的「object」繼承:一個類別繼承問題 為什麼要從 Python 中的「object」繼承:一個類別繼承問題

24 Dec 2024

了解 Python 類別繼承在 Python 中,類別可以從其他類別繼承,這使它們能夠存取父類別的屬性並...

如何在 Linux 上建立和使用動態共用 C 類別庫? 如何在 Linux 上建立和使用動態共用 C 類別庫?

05 Dec 2024

在 Linux 上建立和使用動態共用 C 類別庫建立共用類別庫要建立共用 C 類別庫,您可以依照...

PHP5試用(二)_PHP教程 PHP5試用(二)_PHP教程

21 Jul 2016

PHP5試用(二)。抽象類別 抽象類別不能被實例化。 抽象類別與其它類別一樣,允許定義變數及方法。 抽象類別同樣可以定義一個抽象的方法,抽象類別的方法不會被執

為什麼無法在字段初始化程序中初始化非靜態字段? 為什麼無法在字段初始化程序中初始化非靜態字段?

03 Jan 2025

了解欄位初始化問題在您的程式碼中,您有一個儲存庫類別 (DinnerRepository) 和一個服務類別 (Service),它使用...

See all articles