日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php xml 互相转换

發(fā)布時(shí)間:2025/4/14 php 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php xml 互相转换 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
php xml 互相轉(zhuǎn)換

正好昨天才做過類似的需求……幾行代碼就可以搞定。

如果你使用 curl 獲取的 xml data
$xml = simplexml_load_string($data);
$data['tk'] = json_decode(json_encode($xml),TRUE);

如果是直接獲取 URL 數(shù)據(jù)的話
$xml = simplexml_load_file($data);
$data['tk'] = json_decode(json_encode($xml),TRUE);

先把 simplexml 對(duì)象轉(zhuǎn)換成 json,再將 json 轉(zhuǎn)換成數(shù)組。

?

?

xml與數(shù)組互轉(zhuǎn)??

2007-12-18 12:29:37|??分類: Xml |舉報(bào) |字號(hào)?訂閱

//如果亂碼的話更改header函數(shù)

<?php //header('Content-type: text/html;charset=utf-8');
class xmlarray
{

??? private $xml = '';//用于讀取xml的變量
??? private $data;??? //生成的xml
??? private $dom_tree;//xml目錄樹
/**
?? __construct僅用于讀取xml
*/
??? function __construct($xml="")
??? {
?? if(empty($xml))
?? {
??? return null;
?? }
?? //$this->xml = $xml;
?? else
?? {
???????????? $this->loadxml($xml);
?? }
??? }
/**
???? 裝載要處理的xml文檔也可以在初始化時(shí)裝載
*/
public function loadxml($filepath)
{??
????? $handle = @fopen($filepath,"r");

???????? while (!feof($handle)&&$handle)
?? {
????????????? $xml_data .= fgets($handle, 4096);
???????? }
?? $this->xml=$xml_data;
}
/**
???? 處理xml文檔
*/
??? private function _struct_to_array($values, &$i)
??? {
?? $child = array();
?? if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']);

?? while ($i++ < count($values))
?? {
??? switch ($values[$i]['type'])
??? {
???? case 'cdata':
????? array_push($child, $values[$i]['value']);
????? break;

???? case 'complete':
????? $name = $values[$i]['tag'];
????? if(!empty($name))
????? {
?????? $child[$name]= ($values[$i]['value'])?($values[$i]['value']):'';
?????? if(isset($values[$i]['attributes']))
?????? {
??????? $child[$name] = $values[$i]['attributes'];
?????? }
????? }
????? break;

???? case 'open':
????? $name = $values[$i]['tag'];
????? $size = isset($child[$name]) ? sizeof($child[$name]) : 0;
????? $child[$name][$size] = $this->_struct_to_array($values, $i);
???? break;

???? case 'close':
????? return $child;
????? break;
??? }
?? }
?? return $child;
??? }
/**
??? 處理xml文檔,實(shí)際調(diào)用 _struct_to_array()處理
*/
??? public function xml2array()
??? {
??????? $xml??? =& $this->xml;
??????? $values = array();
??????? $index = array();
??????? $array = array();
??????? $parser = xml_parser_create();
??????? xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
??????? xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
??????? xml_parse_into_struct($parser, $xml, $values, $index);

??????? xml_parser_free($parser);
??????? $i = 0;
??????? $name = $values[$i]['tag'];
??????? $array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : '';
??????? $array[$name] = $this->_struct_to_array($values, $i);
??????? return $array;
??? }


??? //以下為將數(shù)組轉(zhuǎn)換成xml的代碼 使用dom
??????? /**
???? 讀取數(shù)組
???????? */
??????? public function array2xml($array){
??????????? if(!is_array($array)){
??????????????? throw new Exception('array2xml requires an array', 1);
??????????????? unset($array);
??????????? }
??????????? if(!count($array)){
??????????????? throw new Exception('array is empty', 2);
??????????????? unset($array);
??????????? }
???????????
??????????? $this->data = new DOMDocument();?????????
??????????? $this->dom_tree = $this->data->createElement('result');//默認(rèn)要標(biāo)簽
??????????? $this->data->appendChild($this->dom_tree);
??????????? $this->recurse_node($array, $this->dom_tree);
??????? }
???????
??????? /**
???????????? 處理數(shù)組為xml
???????? */
??????? private function recurse_node($data, $obj){
??????????? $i = 0;
??????????? foreach($data as $key=>$value){
??????????????? if(is_array($value)){
??????????????????? //recurse if neccisary
??????????????????? $sub_obj[$i] = $this->data->createElement($key);//創(chuàng)建標(biāo)簽
??????????????????? $obj->appendChild($sub_obj[$i]); //將標(biāo)簽加入到$obj標(biāo)簽下
??????????????????? $this->recurse_node($value, $sub_obj[$i]); //將值加入此標(biāo)簽下
??????????????? } elseif(is_object($value)) {
??????????????????? //no object support so just say what it is
??????????????????? $sub_obj[$i] = $this->data->createElement($key, 'Object: "' . $key . '" type: "' . get_class($value) . '"');
??????????????????? $obj->appendChild($sub_obj[$i]);
??????????????? } else {
??????????????????? //straight up data, no weirdness
??????????????????? $sub_obj[$i] = $this->data->createElement($key, $value);
????? //如果是最后的節(jié)點(diǎn),將節(jié)點(diǎn)名和內(nèi)容創(chuàng)建
??????????????????? $obj->appendChild($sub_obj[$i]);
??????????????? }
??????????????? $i++;
??????????? }
??????? }
???????
??????? /**
       將數(shù)組保存成xml文件
???????? */
??????? public function saveXML($arraytoxml="")
?? {
??? if($arraytoxml=="")
??? {
???? $out = iconv("gbk","utf-8","請(qǐng)輸入將要保存的文件名");
???? echo "<script>alert('$out')</script>";
??? }
??? else
??? {
???????????????? return $this->data->save($arraytoxml);
??? }
??????? }
}
?>


將數(shù)組轉(zhuǎn)換成xml文件
<?php
?? $test = array(
??????? 'one'=>'number',
??????? 'two'=> array(
??????????? 'c'=>1,
??????????? 'd'=>2,
??????????? 'e'=>3,
??????????? 'f'=>4
??????? ),
??????? 'three'=>'alpha'
??? );
?? /*創(chuàng)建對(duì)象*/
?? $xmlObj= new xmlarray();
?? /*轉(zhuǎn)換*/
?? $xml = $xmlObj->array2xml($test);
?? /*輸出保存*/
?? $xmlObj->saveXML("a.xml");
?? ?>


將xml解析為數(shù)組輸出
<?php
?? /*創(chuàng)建對(duì)象*/
?? $xmlObj??? = new xmlarray();
?? /*裝載*/
?? $xml = $xmlObj->loadxml("a.xml");
?? /*轉(zhuǎn)換*/
?? $data=$xmlObj->xml2array();
?? /*顯示*/
?? print_r($data);
?>

?

posted on 2014-08-03 10:43 jason&li 閱讀(...) 評(píng)論(...) 編輯 收藏

轉(zhuǎn)載于:https://www.cnblogs.com/ldms/p/xml.html

總結(jié)

以上是生活随笔為你收集整理的php xml 互相转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。