本文实例讲述了PHP读取并输出XML文件数据的简单实现方法。分享给大家供大家参考,具体如
本文实例讲述了PHP读取并输出XML文件数据的简单实现方法。分享给大家供大家参考,具体如下:
config.XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<node>
<student>
<name>张明</name>
<email>1234567890@qq.com</email>
<username>一样菜</username>
<code>985931</code>
</student>
<student>
<name>王红</name>
<email>2345678901@qq.com</email>
<username>冰封</username>
<code>5625362</code>
</student>
</node>
php文件:
<?php
$file = 'config/config.xml';
$xml_array=simplexml_load_file($file); //将XML中的数据,读取到数组对象中
foreach($xml_array as $tmp){
echo $tmp->name.": ".$tmp->email.", ".$tmp->username.", ".$tmp->code."<br>";
}
?>
结果
张明: 1234567890@qq.com, 一样菜, 985931
王红: 2345678901@qq.com, 冰封, 5625362
PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线XML/JSON互相转换工具: 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
PHP 读取 输出 XML文件数据