PHP simplexml使用方法详解
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-20 13:48:29 浏览: 评论:0
本文章给来大家讲一下关于PHP下simplexml使用方法,希望通过此文章能提升大家对simplexml操作xml文档的理解,有需要了解的朋友可参考.
PHP XML处理XML代码如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Edited with XML Spy v2007 (http://www.phpfensi.com) -->
- <breakfast_menu>
- <food id="1">
- <name>Belgian Waffles</name>
- <price>$5.95</price>
- <description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
- <calories>650</calories>
- </food>
- <food id="2">
- <name>Strawberry Belgian Waffles</name>
- <price>$7.95</price>
- <description>light Belgian waffles covered with strawberries and whipped cream</description>
- <calories>900</calories>
- </food>
- <food id="3">
- <name>Berry-Berry Belgian Waffles</name>
- <price>$8.95</price>
- <description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
- <calories>900</calories>
- </food>
- </breakfast_menu>
php解决xml代码,代码如下:
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>使用simpleXML处理XML</title>
- </head>
- <body>
- <table border="1" cellpadding="0" cellspacing="0" width="700">
- </tbody>
- <tr bgcolor="green">
- <th width="5%"> </th>
- <th width="20%">name</th>
- <th width="10%">price</th>
- <th width="55%">description</th>
- <th width="10%">calories</th>
- </tr>
- <?php
- // 使用simpleXML处理XML
- $xml = simplexml_load_file('./simple.xml');
- //var_dump($xml);
- //echo $xml->getName();
- //var_dump($xml->children());
- $record = '';
- foreach ($xml->children() as $child)
- {
- $record .= '<tr><td>'. $child->attributes(). '</td>';
- foreach ($child->children() as $item)
- {
- //var_dump($child);
- $record .= '<td>'. $item .'</td>';
- }
- $record .= '</tr>';
- }//开源代码phpfensi.com
- echo $record;
- ?>
- </tbody>
- </table>
- </body>
- </html>
将XML转换为数组结构,代码如下:
- private function change_simple_xml_to_array($obj){
- $i = 0;
- //从根节点下的二层子节点开始遍历
- foreach($obj->children() as $value){
- //如果发生同级、同标签名的情况,则加一维数组;
- if(isset($last_name) && $value->getName()==$last_name){
- //出现同名发现第二个,则推入第二维数组;
- if($i==1) $arr[$last_name]=array($arr[$last_name]);
- //遍历子节点;
- array_push($arr[$last_name],$this->change_simple_xml_to_array($value));
- }else{
- $last_name = $value->getName();
- if(count($value->children())==0) $arr[$last_name] = $value."";
- else $arr[$last_name] = $this->change_simple_xml_to_array($value);
- }
- $i++;
- }
- return $arr;
- }
- //XML文档——————
- <?xml version="1.0" encoding="utf-8"?>
- <use>
- <login id="1">
- <user>bddg</user>
- <pas>abcdefg</pas>
- <permission><fdsa>5</fdsa></permission>
- <online_time>2</online_time>
- </login>
- <login id="2">
- <user>baidongdaogu</user>
- <pas>aaa</pas>
- <permission>1</permission>
- <online_time>2</online_time>
- </login>
- <login id="3">
- <user>baidongdaogu</user>
- <pas>aaa</pas>
- <permission>1</permission>
- <online_time>2</online_time>
- </login>
- </use>
- //转换后的数组————
- Array
- (
- [login] => Array
- (
- [0] => Array
- (
- [user] => bddg
- [pas] => abcdefg
- [permission] => Array
- (
- [fdsa] => 5
- )
- [online_time] => 2
- )
- [1] => Array
- (
- [user] => baidongdaogu
- [pas] => aaa
- [permission] => 1
- [online_time] => 2
- )
- [2] => Array
- (
- [user] => baidongdaogu
- [pas] => aaa
- [permission] => 1
- [online_time] => 2
- )
- )
- )
Tags: simplexml 使用simplexml
相关文章
- ·php5的simplexml解析错误(2013-11-13)
- ·php中simplexml_load_string使用实例(2014-08-20)
- ·php中SimpleXML生成与解析xml文件(2014-08-20)
- ·php simpleXML添加CDATA格式数据(2014-08-20)
- ·php中DOMDocument与SimpleXML创建与解析xml程序(2014-08-20)
- ·php Simplexml_Load_file解析xml详细实例(2014-08-20)
- ·php xmlreader simplexml DOMDocument等读取xml的例子(2015-04-08)
- ·php的SimpleXML方法读写XML接口文件实例解析(2021-02-20)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)