php中simplexml_load_file函数的学习
2016-11-16 22:32
645 查看
php中把xm文件转换为以一个对象,可以使用simplexml_load_file来实现,首先创建一个xml文件为test.xml(文件最好是使用utf-8格式来保存),内容为
接下来就是使用php来转换了,代码如下:
得到的输出结果为:
<?xml version="1.0" encoding="UTF-8" ?> <themeeditor> <author>test</author> <name>testname</name> <layout> <item> <block>整体布局</block> <default>fullwidth</default> <option> <text>宽屏</text> <value>fullwidth</value> </option> <option> <text>中屏</text> <value>boxed-lg</value> </option> <option> <text>小屏</text> <value>boxed-md</value> </option> </item> <item> <block>浏览</block> <default>0</default> <option> <text>启用</text> <value>1</value> </option> <option> <text>关闭</text> <value>0</value> </option> </item> </layout> </themeeditor>
接下来就是使用php来转换了,代码如下:
$out = simplexml_load_file('test.xml' ); var_dump($out);
得到的输出结果为:
object(SimpleXMLElement)#1 (3) { ["author"]=> string(4) "test" ["name"]=> string(8) "testname" ["layout"]=> object(SimpleXMLElement)#2 (1) { ["item"]=> array(2) { [0]=> object(SimpleXMLElement)#3 (3) { ["block"]=> string(12) "整体布局" ["default"]=> string(9) "fullwidth" ["option"]=> array(3) { [0]=> object(SimpleXMLElement)#5 (2) { ["text"]=> string(6) "宽屏" ["value"]=> string(9) "fullwidth" } [1]=> object(SimpleXMLElement)#6 (2) { ["text"]=> string(6) "中屏" ["value"]=> string(8) "boxed-lg" } [2]=> object(SimpleXMLElement)#7 (2) { ["text"]=> string(6) "小屏" ["value"]=> string(8) "boxed-md" } } } [1]=> object(SimpleXMLElement)#4 (3) { ["block"]=> string(6) "浏览" ["default"]=> string(1) "0" ["option"]=> array(2) { [0]=> object(SimpleXMLElement)#7 (2) { ["text"]=> string(6) "启用" ["value"]=> string(1) "1" } [1]=> object(SimpleXMLElement)#6 (2) { ["text"]=> string(6) "关闭" ["value"]=> string(1) "0" } } } } } }
note:simplexml_load_file可以使用第二个参数来返回一个自定义的一个类,但是这个类必须是SimpleXMLElement的子类
相关文章推荐
- php中simplexml_load_file函数用法实例
- php中simplexml_load_file函数用法实例
- simplexml_load_file()将xml转化为php对象的函数
- php中simplexml_load_file()函数用法详细
- php中simplexml_load_file函数用法实例
- PHP中Simplexml_Load_file解析xml实例
- simplexml_load_file()函数
- 在使用simplexml_load_file()函数读取xml文件时遇到<![CDATA[]]>,怎么让其进行解析
- PHP-问题处理Fatal error: Uncaught Error: Call to undefined function simplexml_load_file()
- php中simplexml_load_file中CDATA数据不显示的问题
- PHP simplexml_load_string() 函数
- php使用simplexml_load_file加载XML文件并显示XML的方法
- simplexml_load_file()php解析xml文档
- MYSQL注入中load_file()函数的进一步应用
- php学习笔记1--根据条件调用不同的函数
- PHP 5 SimpleXML 函数
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
- PHP可变函数学习小结
- Lua 加载函数require dofile loadfile
- php学习第二章:字符串处理函数(一)