php 获取xml内容,php读取xml内容

xml如下:

http://www.hu-rong.com

www.hu-rong.com

转为数据结构如下: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

$xml=simplexml_load_file('test.xml');

//print_r(json_decode(json_encode($xml),true));

echo $xml->getName().'节点下有'.$xml->count().'个item
';

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].'
';

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:

$xml=simplexml_load_file('test.xml');

//print_r(json_decode(json_encode($xml),true));

$count=$xml->count();

echo $xml->getName().'节点下有'.$count.'个item
';

$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.'
';

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值