现在的位置: 首页 > 综合 > 正文

php中xpath查找xml的节点和节点属性值

2013年07月26日 ⁄ 综合 ⁄ 共 335字 ⁄ 字号 评论关闭

今天群里有人问,顺便实验了一下。

<?php

$xml = "
<node>
    <title src='hello'>world</title>
</node>
";

function get_data_from_xml($res_data,$node)
{
    $xml = simplexml_load_string($res_data);
    $result = $xml->xpath($node);

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

// 'hello world';

echo get_data_from_xml($xml, '/node/title/@src[.]'), ' ' , get_data_from_xml($xml, '/node/title');

抱歉!评论已关闭.