PHP簡單實(shí)現(xiàn)解析xml為數(shù)組的方法
本文實(shí)例講述了PHP簡單實(shí)現(xiàn)解析xml為數(shù)組的方法。分享給大家供大家參考,具體如下:
最近想要做一個(gè)插件機(jī)制,需要用到xml,在解析xml時(shí)候需要轉(zhuǎn)換為數(shù)組,特意記錄一個(gè)此種解析方式
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è)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP對象的淺復(fù)制與深復(fù)制的實(shí)例詳解
這篇文章主要介紹了PHP對象的淺復(fù)制與深復(fù)制的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10PHP微信開發(fā)用Cache 解決數(shù)據(jù)緩存
本文主要介紹,解決PHP微信開發(fā)時(shí)數(shù)據(jù)緩存的問題,這里用Cache 類舉例說明,具有參考價(jià)值,感興趣的小伙伴可以參考下2016-07-07PHP 5.3 下載時(shí) VC9、VC6、Thread Safe、Non Thread Safe的區(qū)別分析
我最近在 PHP 官網(wǎng)上看到又有新版的 PHP 下載了,于是上去找找 For Windows 的版本,可是一看確傻眼了2011-03-03PHP中include()與require()的區(qū)別說明
引用文件的方法有兩種:require 及 include。兩種方式提供不同的使用彈性。2010-03-03php自定義函數(shù)實(shí)現(xiàn)統(tǒng)計(jì)中文字符串長度的方法小結(jié)
這篇文章主要介紹了php自定義函數(shù)實(shí)現(xiàn)統(tǒng)計(jì)中文字符串長度的方法,結(jié)合實(shí)例形式總結(jié)分析了php針對中文的判定、編碼與運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-04-04php 數(shù)組排序 array_multisort與uasort的區(qū)別
其實(shí),簡單的說兩者的最重要的區(qū)別:array_multisort()排序之后,原數(shù)組“鍵”丟失。uasort()排序之后,保留原來的“鍵”。2011-03-03