如何簡單的將xml格式資料轉換成array資料

2021-08-26 05:39:25 字數 1513 閱讀 7072

對於將xml格式資料轉換成array資料,很多初學者應該都不知道,那麼今天就來為大家分析解答一下吧。

php中的$_post和file_get_content('php://input')

$_post

老規矩,檢視官方文件,全世界都會騙你,但是文件不會騙你。

php://input

php:// — 訪問各個輸入/輸出流(i/o streams)php 提供了一些雜項輸入/輸出(io)流,允許訪問 php 的輸入輸出流、標準輸入輸出和錯誤描述符, 記憶體中、磁碟備份的臨時檔案流以及可以操作其他讀取寫入檔案資源的過濾器。php://input 是個可以訪問請求的原始資料的唯讀流。 post 請求的情況下,最好使用 php://input 來代替 $http_raw_post_data,因為它不依賴於特定的 php.ini 指令。 而且,這樣的情況下 $http_raw_post_data 預設沒有填充, 比啟用 always_populate_raw_post_data 潛在需要更少的記憶體。 enctype="multipart/form-data" 的時候 php://input 是無效的。note: 在 php 5.6 之前 php://input 開啟的資料流只能讀取一次; 資料流不支援 seek 操作。 不過,依賴於 sapi 的實現,請求體資料被儲存的時候, 它可以開啟另乙個 php://input 資料流並重新讀取。 通常情況下,這種情況只是針對 post 請求,而不是其他請求方式,比如 put 或者 propfind。

parserequest

request類其實是很好寫的,但是一般的類中對post方式傳遞引數還是老方式。因此,我改寫了一般的類中獲取post的引數,對於所有post請求方式的方法,針對不同的content-type進行資料獲取和原始資料的解析,弄成我們想要的最終陣列。

核心**:

public static function post($key = null, $default = null) if(in_array($_server['content_type'],self::$formats['xml'])) if($key==null && !empty($data))if(!empty($data)) return static::lookup($_post, $key, $default); }

在類中我們會定義幾個conten-type的format陣列,通過$_server['content_type']來進行判斷處理,針對性的進行資料獲取和轉換。

針對xml格式的資料,同時封裝了資料處理類

public static function toarray($xml) // 檢查xml是否合法 $xml_parser = xml_parser_create();if (!xml_parse($xml_parser, $xml, true)) libxml_disable_entity_loader(true); $data = json_decode(json_encode(******xml_load_string($xml, '******xmlelement', libxml_nocdata)), true); return $data; }

簡單將xml資料轉換為txt資料

import xml.etree.elementtree as et import pickle import os from os import listdir,getcwd from os.path import join sets train trainval classes mono def...

php將資料庫中的資料轉換成xml格式的

php將資料庫中的內容轉換成xml格式的 db host localhost 資料庫位置 db user root 使用者名稱 db password 123456 密碼 db name test 資料庫名稱 link mysql connect db host,db user,db passwor...

ue的xml格式轉換 VCARD格式

vcard 規範容許公開交換個人資料交換 personal data interchange pdi 資訊,在傳統紙質商業名片可找到這些資訊。規範定義電子名片 或叫 vcard 的格式。vcard 規範可作為各種應用或系統之間的交換格式。定義的格式與傳 送的方法無關。傳送交換可能是檔案系統,點對點交...