本文实例讲述了PHP简单实现解析xml为数组的方法。分享给大家供大家参考,具体如下:
最近想要做一个插件机制,需要用到xml,在解析xml时候需要转换为数组,特意记录一个此种解析方式
xmlDemo.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <main xmlns="http://www.xiaoetongo.cn" versionCode="1.0"> <controller co="Aritles"> <meth style=\"margin: 0px; padding: 0px; outline: none; line-height: 25.2px; font-size: 14px; width: 660px; overflow: hidden; clear: both; font-family: tahoma, arial, "Microsoft YaHei";\"><?php $xmls=file_get_contents("xmlDemo.xml"); $xml =simplexml_load_string($xmls); $xmljson= json_encode($xml); $xml=json_decode($xmljson,true); var_dump($xml);运行结果:
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) { } }希望本文所述对大家PHP程序设计有所帮助。
相关文章
猜你喜欢
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10