<?php $myData = array(); $file = file_get_contents("test.xml"); if(strpos($file, '<?xml') > -1) { try { //加载解析xml $xml = simplexml_load_string($file); if($xml) { //echo $this->result; //获取节点值 $CONNECT_ID = $xml->CONNECT_ID; $MO_MESSAGE_ID = $xml->MO_MESSAGE_ID; $RECEIVE_DATE = $xml->RECEIVE_DATE; $RECEIVE_TIME = $xml->RECEIVE_TIME; $GATEWAY_ID = $xml->GATEWAY_ID; $VALID = $xml->VALID; $CITY_CODE = $xml->CITY_CODE; $CITY_NAME = $xml->CITY_NAME; $STATE_CODE = $xml->CITY_CODE; $STATE_NAME = $xml->STATE_NAME; $TP_PID = $xml->TP_PID; $TP_UDHI = $xml->TP_UDHI; $MSISDN = $xml->MSISDN; $MESSAGE_TYPE = $xml->MESSAGE_TYPE; $MESSAGE = $xml->MESSAGE;//短信 $LONG_CODE = $xml->LONG_CODE; $SERVICE_CODE = $xml->SERVICE_CODE; preg_match("/(561)\d{1,2}/", $MESSAGE, $code); switch($code[0]) { case 5618 : $myData[message] = $MESSAGE; break; default : $myData[] = '没有短消息。'; break; } } else { echo "加载xml文件错误。"; } } catch(exception $e){ print_r($e); } } else { echo "没有该XML文件。"; } echo "<pre>"; print_r($myData); echo "<hr>"; echo $myData[message]; ?>
PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP针对XML文件操作技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》