欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP实现解析xml为数组案例详解

程序员文章站 2022-04-23 15:58:37
...
这次给大家带来PHP实现解析xml为数组案例详解,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 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);

运行结果:

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计算特大数字的整数集

php使用curl仿制ip与refer步骤详解

以上就是PHP实现解析xml为数组案例详解的详细内容,更多请关注其它相关文章!

相关标签: php 详解 案例