php simplexml 遍历,PHP simplexml使用方法详解

本文章给来大家讲一下关于PHP下simplexml使用方法,希望通过此文章能提升大家对simplexml操作xml文档的理解,有需要了解的朋友可参考.

PHP XML处理XML代码如下:Belgian Waffles

$5.95

two of our famous Belgian Waffles with plenty of real maple syrup

650

Strawberry Belgian Waffles

$7.95

light Belgian waffles covered with strawberries and whipped cream

900

Berry-Berry Belgian Waffles

$8.95

light Belgian waffles covered with an assortment of fresh berries and whipped cream

900

php解决xml代码,代码如下:使用simpleXML处理XMLname

price

description

calories

getName();

//var_dump($xml->children());

$record = '';

foreach ($xml->children() as $child) {

$record.= '

' . $child->attributes() . '';

foreach ($child->children() as $item) {

//var_dump($child);

$record.= '' . $item . '';

}

$record.= '';

}

echo $record;

?>

将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文档——————bddg

abcdefg

5

2

baidongdaogu

aaa

1

2

baidongdaogu

aaa

1

2

//转换后的数组————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

)

)

)

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值