醉丶春风的Blog

千里之行, 始于足下



PHP如何将XML转换为数组


 在很多开发项目中,我们都会遇到将XML文件转换为数组使用,因此在本篇PHP教程中,UncleToo和大家一起学习如何转换XML为数组。

现在有一个uncletoo.xml的配置文件,格式如下:

<?xml version='1.0'?>
 
<moleculedb>
 
    <molecule name='Benzine'>
 
        <symbol>ben</symbol>
 
        <code>A</code>
 
    </molecule>
 
    <molecule name='Water'>
 
        <symbol>h2o</symbol>
 
        <code>K</code>
 
    </molecule>
 
<molecule name='Parvez'>
 
        <symbol>h2o</symbol>
 
        <code>K</code>
 
    </molecule>
 
</moleculedb>

1、读XML文件内容,并保存到字符串变量中

下面我们使用PHP自带的file_get_contents()函数将文件内容读取到一个字符串变量中:$path为xml文件路径

$xmlfile = file_get_contents($path);

    

2、将字符串转换为对象

这一步我们将使用simplexml_load_string()函数,将上一步得到的字符串转换为对象(Object):

$ob= simplexml_load_string($xmlfile);

此时$ob的值如下:

3、将对象转换为JSON

上一步转换成对象后,现在,我们要将对象转换成JSON格式字符串:

$json  = json_encode($ob);

此时$json变量的值如下:

blob.png

4、解析JSON字符串

这也是最后一步了,我们需要将JSON格式的字符串转换为我们需要的数组:

$configData = json_decode($json, true);

现在$configData里存储的数据就是我么最后要得到的数组,如下:

blob.png

完整转换代码:

<?php
    $xmlfile = file_get_contents($path);
    $ob= simplexml_load_string($xmlfile);
    $json  = json_encode($ob);
    $configData = json_decode($json, true);
?>



作者: 徐善通
地址: https://www.xstnet.com/article-46.html
声明: 除非本文有注明出处,否则转载请注明本文地址


我有话说



最新回复


正在加载中....

Copyrights © 2016-2019 醉丶春风 , All rights reserved. 皖ICP备15015582号-1