xml如下:
<?xml version="1.0" encoding="utf-8"?>
<list>
<item index="0">
<name a="a0" b="b0"><![CDATA[张三]]></name>
<site c="c0" d="d0">http://www.hu-rong.com</site>
</item>
<item index="1">
<name a="a1" b="b1"><![CDATA[李四]]></name>
<site c="c1" d="d1">www.hu-rong.com</site>
</item>
</list>
转为数据结构如下:
Array ( [item] => Array ( [0] => Array ( [@attributes] => Array ( [index] => 0 ) [name] => Array ( [@attributes] => Array ( [a] => a0 [b] => b0 ) ) [site] => http://www.hu-rong.com ) [1] => Array ( [@attributes] => Array ( [index] => 1 ) [name] => Array ( [@attributes] => Array ( [a] => a1 [b] => b1 ) ) [site] => www.hu-rong.com ) ) )
奇怪的是site的属性丢了
php读xml
<?php
$xml=simplexml_load_file('test.xml');
//print_r(json_decode(json_encode($xml),true));
echo $xml->getName().'节点下有'.$xml->count().'个item<br/>';
foreach($xml as $item){
$str='';
$child=$item->children();
$attributes=$child->attributes();
$str.='index为'.$item->attributes()->index.'item下有';
$str.=$child[0]->getName().'(属性'.$attributes[0]->getName().'=>'.$attributes[0].','.$attributes[1]->getName().'=>'.$attributes[1].')和';
$str.=$child[1]->getName().'节点';
$str.='其值分别为:'.$item->name.','.$child[1].'<br/>';
echo $str;
}
结果如下:
index为0item下有name(属性a=>a0,b=>b0)和site节点其值分别为:张三,http://www.hu-rong.com
index为1item下有name(属性a=>a1,b=>b1)和site节点其值分别为:李四,www.hu-rong.com
list节点下有2个item
例2:
<?php
$xml=simplexml_load_file('test.xml');
//print_r(json_decode(json_encode($xml),true));
$count=$xml->count();
echo $xml->getName().'节点下有'.$count.'个item<br/>';
$i=0;
foreach($xml as $item){
$str='';
$child=$item->children()[$i];
$attributes=$child->attributes();
$str.='index为'.$item->attributes()->index.'的item下的';
$str.=$child->getName().'(属性'.$attributes->getName().'=>'.$attributes[0].','.$attributes[1]->getName().'=>'.$attributes[1].')';
$str.='其值分别为:'.$child.'<br/>';
echo $str;
$i++;
}
运行结果:
index为0的item下的name(属性a=>a0,b=>b0)其值分别为:张三
index为1的item下的site(属性c=>c1,d=>d1)其值分别为:www.hu-rong.com
list节点下有2个item
- 相关文章
- phpexcel设置行高及列宽,背景颜色,
- 单点登录sso原理及php实现方式及de
- 在php中用curl请求java接口无法获
- 免费ip地址查询接口
- php,java,android, aes加解密-加密
- 地图上计算两点间的距离
- 关于百度地图的一些技巧
- php中empty,is_null,isset的区别
- windows及linux下composer安装教程
- 一种精妙的数据查询与存储方式
- 热门文章
- win7中将文件拷贝到虚拟机linux下
- phpexcel设置行高及列宽,背景颜色,
- rabbitmq无法启动
- intellij idea不显示git push按钮
- php7中使用mongodb的aggregate进行
- centos7.4 64位下swoole安装及配置
- laravel页面静态化的方法
- navicate连接mycat报1184错误
- 单点登录sso原理及php实现方式及de
- devops-jenkins容器为pending状态
- 好评文章
- phpexcel设置行高及列宽,背景颜色,
- php7中使用mongodb的aggregate进行
- intellij idea打开文件所在文件夹
- windows下使用MongoDB Compass Com
- win7中将文件拷贝到虚拟机linux下
- laravel 中悲观锁 & 乐观锁的使用
- 单点登录sso原理及php实现方式及de
- navicate连接mycat报1184错误
- rabbitmq无法启动
- laravel整合dingo/api方法步骤:jwt
- 我的项目
- 【github】www.github.com/hurong241
- 【码云】gitee.com/hu_rong/projects
- 【docker hub】hub.docker.com/repositories/hurong241
- 【packagist】packagist.org/users/hurong241/packages
- 站点信息
- 建站时间:2011年
- 文章数:610篇
- 浏览数:953068