码迷,mamicode.com
首页 > 其他好文 > 详细

xml simpleXML_load_file(), simpleXML_load_string()

时间:2014-07-30 00:36:12      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:http   os   文件   io   for   cti   ar   res   

xml.xml文件

<?xml version=‘1.0‘?>
<man>
    <att>
        <name>lin3615</name>
        <sex>M</sex>
        <age>26</age>
    </att>
    <att>
        <name>lin361500</name>
        <sex>mmm</sex>
        <age>20</age>
    </att>
</man>

用simpleXML_load_file()实现

<?php
$ff = ‘http://localhost/test.xml‘;
$str = simpleXML_load_file($ff);
print_r($str);
foreach($str->att as $v) print_r($v);

function get_contents($url){
     if (ini_get("allow_url_fopen") == "1") {
                $response = file_get_contents($url);
        }else{
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                curl_setopt($ch, CURLOPT_URL, $url);
                $response =  curl_exec($ch);
                curl_close($ch);
        }

        return $response;
}

用simpleXML_load_string()实现

<?php
$ff = get_contents("http://localhost/test/test.xml");
$str = simpleXML_load_string($ff);
print_r($str);
foreach($str->att as $v) print_r($v);

function get_contents($url){
     if (ini_get("allow_url_fopen") == "1") {
                $response = file_get_contents($url);
        }else{
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                curl_setopt($ch, CURLOPT_URL, $url);
                $response =  curl_exec($ch);
                curl_close($ch);
        }

        return $response;
}

结果都为:

SimpleXMLElement Object
(
    [att] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [name] => lin3615
                    [sex] => M
                    [age] => 26
                )

            [1] => SimpleXMLElement Object
                (
                    [name] => lin361500
                    [sex] => mmm
                    [age] => 20
                )

        )

)
SimpleXMLElement Object
(
    [name] => lin3615
    [sex] => M
    [age] => 26
)
SimpleXMLElement Object
(
    [name] => lin361500
    [sex] => mmm
    [age] => 20
)

xml simpleXML_load_file(), simpleXML_load_string(),布布扣,bubuko.com

xml simpleXML_load_file(), simpleXML_load_string()

标签:http   os   文件   io   for   cti   ar   res   

原文地址:http://www.cnblogs.com/lin3615/p/3876669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!