首页 > PHP 如何得到simplexml_load_string 的值

PHP 如何得到simplexml_load_string 的值

问题

/**
 * 通过节点路径返回字符串的某个节点值
 * $res_data——XML 格式字符串
 * 返回节点参数
 */
function getDataForXML($res_data, $node)
{
    $xml = simplexml_load_string($res_data);
    $result = $xml->xpath($node);


    while (list(, $node) = each($result)) {
        return $node;
    }
}

$_POST = array(
    'service'     => 'alipay.wap.trade.create.direct',
    'sign'        => 'db56d137c71f591abd58b41d5da5f920',
    'sec_id'      => 'MD5',
    'v'           => '1.0',
    'notify_data' => '<notify><payment_type>1</payment_type><subject>充值</subject><trade_no>2014123069117121</trade_no><buyer_email>default@gmail.com</buyer_email><gmt_create>2014-12-30 22:12:32</gmt_create><notify_type>trade_status_sync</notify_type><quantity>1</quantity><out_trade_no>1412302289443</out_trade_no><notify_time>2014-12-30 22:12:56</notify_time><seller_id>2088211987518229</seller_id><trade_status>TRADE_FINISHED</trade_status><is_total_fee_adjust>N</is_total_fee_adjust><total_fee>0.11</total_fee><gmt_payment>2014-12-30 22:12:56</gmt_payment><seller_email>zfb@gmail.cn</seller_email><gmt_close>2014-12-30 22:12:56</gmt_close><price>0.11</price><buyer_id>2088102193791211</buyer_id><notify_id>8f3d3703e293b95d8de26cbd51d74fe116</notify_id><use_coupon>N</use_coupon></notify>',
);


$out_trade_no = getDataForXML($_POST['notify_data'],'/notify/out_trade_no');
var_dump( ($out_trade_no == '1412302289443' )); //true
var_dump($out_trade_no);//object(SimpleXMLElement)[2]

如何才可以得到 $out_trade_no 的值


$doc = new DOMDocument();
$doc->loadXML($_POST['notify_data']);
$doc->getElementsByTagName( "out_trade_no" )->item(0)->nodeValue;

simplexml解析得到的是simpleElement对象,需要类型转换,如公子所说的方法即可


var_dump( (String)$out_trade_no ); 
【热门文章】
【热门文章】