zoukankan      html  css  js  c++  java
  • 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
    )

  • 相关阅读:
    十天冲刺个人博客四
    十天冲刺个人博客三
    十天冲刺个人博客二
    软件工程课堂七(单词统计)
    十天冲刺个人博客一
    软件工程第九周总结
    人月神话阅读笔记03
    软件工程课堂六(《飘》单词统计)
    软件工程第八周总结
    跨域
  • 原文地址:https://www.cnblogs.com/lin3615/p/3876669.html
Copyright © 2011-2022 走看看