将XML文档内的内容转化为PHP数组

*                          将xml文档内的内容转化为php数组的两种方法
 *
 *
 * $url xml文档路径         必须以http://开头
 * 接口:路径
 */
    $url = "http://www.wamp.com/test/xml.xml";

/**
 * 一:simplexml_load_file()  需要将文件的路径
 *
 *$data->student[0] 找对象中的某个值    ->
 *json_encode() 将数组转化为json格式
 * json_decode()将json对象转化为数组
 *<![CDATA[测试数据]]>      xml文件含有特殊字符 simplexml_load_file($url,"SimpleXMLElement",LIBXML_NOCDATA)
 */
    $data = json_decode(json_encode(simplexml_load_file($url,"SimpleXMLElement",LIBXML_NOCDATA)),true);
/**
 * 二:simplexml_load_string();   需要字符串
 *file_get_contents($url)   读取文件路径  将路径读取为字符串
*/
$url = file_get_contents($url);
$data = json_decode(json_encode(simplexml_load_string($url,"SimpleXMLElement",LIBXML_NOCDATA)),true);

猜你喜欢

转载自blog.csdn.net/sallycnn/article/details/79222203