Home  >  Article  >  Backend Development  >  simplexmlelement - PHP 如何得到simplexml_load_string 的值

simplexmlelement - PHP 如何得到simplexml_load_string 的值

WBOY
WBOYOriginal
2016-06-06 20:39:41960browse

问题

<code>/**
 * 通过节点路径返回字符串的某个节点值
 * $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]
</code>

如何才可以得到 $out_trade_no 的值

回复内容:

问题

<code>/**
 * 通过节点路径返回字符串的某个节点值
 * $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]
</code>

如何才可以得到 $out_trade_no 的值

<code>var_dump( (String)$out_trade_no ); 
</code>

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

<code>$doc = new DOMDocument();
$doc->loadXML($_POST['notify_data']);
$doc->getElementsByTagName( "out_trade_no" )->item(0)->nodeValue;
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn