PHP簡單實(shí)現(xiàn)解析xml為數(shù)組的方法
本文實(shí)例講述了PHP簡單實(shí)現(xiàn)解析xml為數(shù)組的方法。分享給大家供大家參考,具體如下:
最近想要做一個插件機(jī)制,需要用到xml,在解析xml時候需要轉(zhuǎn)換為數(shù)組,特意記錄一個此種解析方式
xmlDemo.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <main xmlns="http://www.xiaoetongo.cn" versionCode="1.0"> <controller co="Aritles"> <meth title="測試插件" do="aritle"/> </controller> <controller co="Ari"> <meth title="測試插件" do="ar"/> <meth title="測試插件" do="a"/> </controller> <install><![CDATA[]]></install> <upgrade><![CDATA[]]></upgrade> </main>
php代碼:
<?php
$xmls=file_get_contents("xmlDemo.xml");
$xml =simplexml_load_string($xmls);
$xmljson= json_encode($xml);
$xml=json_decode($xmljson,true);
var_dump($xml);
運(yùn)行結(jié)果:
array(4) {
["@attributes"]=>
array(1) {
["versionCode"]=>
string(3) "1.0"
}
["controller"]=>
array(2) {
[0]=>
array(2) {
["@attributes"]=>
array(1) {
["co"]=>
string(7) "Aritles"
}
["meth"]=>
array(1) {
["@attributes"]=>
array(2) {
["title"]=>
string(12) "測試插件"
["do"]=>
string(6) "aritle"
}
}
}
[1]=>
array(2) {
["@attributes"]=>
array(1) {
["co"]=>
string(3) "Ari"
}
["meth"]=>
array(2) {
[0]=>
array(1) {
["@attributes"]=>
array(2) {
["title"]=>
string(12) "測試插件"
["do"]=>
string(2) "ar"
}
}
[1]=>
array(1) {
["@attributes"]=>
array(2) {
["title"]=>
string(12) "測試插件"
["do"]=>
string(1) "a"
}
}
}
}
}
["install"]=>
array(0) {
}
["upgrade"]=>
array(0) {
}
}
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
ThinkPHP3.2.3所有的單字母方法整理總結(jié)
ThinkPHP3.2.3在以前是一款廣受歡迎的PHP開發(fā)框架,現(xiàn)在雖然除了版本5和6,但仍有不少項目使用ThinkPHP3。ThinkPHP3中的字母函數(shù)是以字母開頭的一類函數(shù),其作用是非常豐富的,包括字符串處理、文件處理、日期時間處理以及HTTP協(xié)議處理等。2023-07-07
PHP extract 將數(shù)組拆分成多個變量的函數(shù)
extract()函數(shù)提取關(guān)聯(lián)數(shù)組(對數(shù)字索引數(shù)組無效)每對key和value,生成以key為變量名、value為對應(yīng)值的多組新變量。2010-06-06

