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
相關文章
為什麼要從 Python 中的「object」繼承:一個類別繼承問題
24 Dec 2024
了解 Python 類別繼承在 Python 中,類別可以從其他類別繼承,這使它們能夠存取父類別的屬性並...
PHP5試用(二)_PHP教程
21 Jul 2016
PHP5試用(二)。抽象類別 抽象類別不能被實例化。 抽象類別與其它類別一樣,允許定義變數及方法。 抽象類別同樣可以定義一個抽象的方法,抽象類別的方法不會被執
為什麼無法在字段初始化程序中初始化非靜態字段?
03 Jan 2025
了解欄位初始化問題在您的程式碼中,您有一個儲存庫類別 (DinnerRepository) 和一個服務類別 (Service),它使用...
Hot Tools
熱門文章
崩壞:星穹鐵道 - 所有金色替罪羊謎題解決方案
18 Jan 2025
手游攻略
Tales Of Graces F 重製版:所有鎖定的寶箱密碼
18 Jan 2025
手游攻略
魯馬島:考古學家職業指南
03 Jan 2025
手游攻略
如何修復 KB5049622 在 Windows 11 中安裝失敗的問題?
15 Jan 2025
故障排查
魯瑪島:所有魯瑪蛋地點
05 Jan 2025
手游攻略