simplexml_load_string 解析xml

<?php
//simplexml_load_string 解析两种类型的xml
$res='<?xml version="1.0" encoding="UTF-8"?>
<SYNCPacket>
<mor><cpid>010001000249</cpid><mid>1009011719781282</mid><cpmid>1411957176</cpmid><mobile>13882524228</mobile><port>0096</port><msg>DELIVRD</msg><area>四川</area><city>遂宁</city><type>4</type><channel>1</channel><reserved></reserved></mor>
<mor><cpid>010001000249</cpid><mid>1009011719781301</mid><cpmid>1411957175</cpmid><mobile>18719295258</mobile><port>0096</port><msg>DELIVRD</msg><area>广东</area><city>阳江</city><type>4</type><channel>1</channel><reserved></reserved></mor>
</SYNCPacket>';
$reStat=simplexml_load_string($res);

foreach ($reStat->children() as $value) {
$arr['restatus']=trim($value->msg);
$arr['mobile']=trim($value->mobile);
$arr['taskid']=trim($value->cpmid) ;

$reply_arr[]=$arr;


}
print_r($reply_arr);


$re='<?xml version="1.0" encoding="UTF-8"?>
<SYNCPacket>
<count>200</count>
<result>0</result>
<report>
<mor><cpid>010001000249</cpid><mid>1009011719781282</mid><cpmid>1411957176</cpmid><mobile>13882524228</mobile><port>0096</port><msg>DELIVRD</msg><area>四川</area><city>遂宁</city><type>4</type><channel>1</channel><reserved></reserved></mor>
<mor><cpid>010001000249</cpid><mid>1009011719781301</mid><cpmid>1411957175</cpmid><mobile>18719295258</mobile><port>0096</port><msg>DELIVRD</msg><area>广东</area><city>阳江</city><type>4</type><channel>1</channel><reserved></reserved></mor>
</report>
</SYNCPacket>';

$reStat=simplexml_load_string($re);

foreach ($reStat->report->mor as $value) {
$arr['restatus']=trim($value->msg);
$arr['mobile']=trim($value->mobile);
$arr['taskid']=trim($value->cpmid) ;

$reply_arr[]=$arr;


}
print_r($reply_arr);


$xml='<xml name="sendBatch" result="1">
<Item cid="333" sid="333" msgid="111" total="1" price="0.10" remain="170.040"/>
  <Item cid="444" sid="444" msgid="222" total="1" price="0.10" remain="169.940"/>
</xml>';
$re=simplexml_load_string(utf8_encode($xml));
if($re['result']==1)
{
foreach ($re->Item as $item)
{
$stat['msgid'] =trim((string)$item['msgid']);
$stat['total']=trim((string)$item['total']);
$stat['price']=trim((string)$item['price']);
$stat['remain']=trim((string)$item['remain']);
$stat_arr[]=$stat;

}
print_r($stat_arr);

}
?>

转载于:https://www.cnblogs.com/cwl168/p/4016255.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值