• xml simpleXML_load_file(), simpleXML_load_string()


    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
    )

  • 相关阅读:
    Python 中的 None 与真假
    AVR第5课:蜂鸣器
    Solr使用入门指南
    EJB究竟是什么,真的那么神奇吗??
    Android 各个版本号WebView
    android SQLite 使用实例
    BackTrack5 (BT5)无线password破解教程之WPA/WPA2-PSK型无线password破解
    腾讯QQ企业邮箱POP3/SMTP设置
    【LeetCode】Substring with Concatenation of All Words
    PreferenceFragment 使用 小结
  • 原文地址:https://www.cnblogs.com/lin3615/p/3876669.html
Copyright © 2020-2023  润新知