php xml 互相转换
正好昨天才做過類似的需求……幾行代碼就可以搞定。
如果你使用 curl 獲取的 xml data
$xml = simplexml_load_string($data);
$data['tk'] = json_decode(json_encode($xml),TRUE);
如果是直接獲取 URL 數據的話
$xml = simplexml_load_file($data);
$data['tk'] = json_decode(json_encode($xml),TRUE);
先把 simplexml 對象轉換成 json,再將 json 轉換成數組。
?
?
xml與數組互轉??
2007-12-18 12:29:37|??分類: Xml |舉報 |字號?訂閱
//如果亂碼的話更改header函數
<?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文檔也可以在初始化時裝載
*/
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文檔,實際調用 _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;
??? }
??? //以下為將數組轉換成xml的代碼 使用dom
??????? /**
???? 讀取數組
???????? */
??????? 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');//默認要標簽
??????????? $this->data->appendChild($this->dom_tree);
??????????? $this->recurse_node($array, $this->dom_tree);
??????? }
???????
??????? /**
???????????? 處理數組為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);//創建標簽
??????????????????? $obj->appendChild($sub_obj[$i]); //將標簽加入到$obj標簽下
??????????????????? $this->recurse_node($value, $sub_obj[$i]); //將值加入此標簽下
??????????????? } 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);
????? //如果是最后的節點,將節點名和內容創建
??????????????????? $obj->appendChild($sub_obj[$i]);
??????????????? }
??????????????? $i++;
??????????? }
??????? }
???????
??????? /**
將數組保存成xml文件
???????? */
??????? public function saveXML($arraytoxml="")
?? {
??? if($arraytoxml=="")
??? {
???? $out = iconv("gbk","utf-8","請輸入將要保存的文件名");
???? echo "<script>alert('$out')</script>";
??? }
??? else
??? {
???????????????? return $this->data->save($arraytoxml);
??? }
??????? }
}
?>
將數組轉換成xml文件
<?php
?? $test = array(
??????? 'one'=>'number',
??????? 'two'=> array(
??????????? 'c'=>1,
??????????? 'd'=>2,
??????????? 'e'=>3,
??????????? 'f'=>4
??????? ),
??????? 'three'=>'alpha'
??? );
?? /*創建對象*/
?? $xmlObj= new xmlarray();
?? /*轉換*/
?? $xml = $xmlObj->array2xml($test);
?? /*輸出保存*/
?? $xmlObj->saveXML("a.xml");
?? ?>
將xml解析為數組輸出
<?php
?? /*創建對象*/
?? $xmlObj??? = new xmlarray();
?? /*裝載*/
?? $xml = $xmlObj->loadxml("a.xml");
?? /*轉換*/
?? $data=$xmlObj->xml2array();
?? /*顯示*/
?? print_r($data);
?>
?
posted on 2014-08-03 10:43 jason&li 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/ldms/p/xml.html
總結
以上是生活随笔為你收集整理的php xml 互相转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows下载Android源代码
- 下一篇: 线段树(成段更新,区间求和lazy操作