php操作xml

1.读取

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;
    }
}

$data = '<?xml version="1.0" encoding="UTF-8"?>
        <package uid="NewFEId_1">
                <response type="launch_account" id="1384221706736_1">
                        <result>0</result>
                </response>
        </package>';
//读取属性
$uid = self::get_data_from_xml($data, '/package/@uid')->uid;
$type = self::get_data_from_xml($data, '/package/response/@type')->type;
$response_id = self::get_data_from_xml($data, '/package/response/@id')->id;
//读取值
$result = self::get_data_from_xml($data, '/package/response/result');


你可能感兴趣的:(php操作xml)