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()的更多相关文章

  1. simplexml_load_string 解析xml

    <?php //simplexml_load_string 解析两种类型的xml $res='<?xml version="1.0" encoding="UT ...

  2. simplexml_load_string获取xml节点里的属性值

    http://stackoverflow.com/questions/14359658/get-xml-attribute-using-simplexml-load-string 问: I am us ...

  3. php将xml文件转化为数组:simplexml_load_string

    <?php $str = <<<XML <?xml version="1.0" encoding="ISO-8859-1"?> ...

  4. PHP将XML转成数组

    如果你使用 curl 获取的 xml data$xml = simplexml_load_string($data);$data['tk'] = json_decode(json_encode($xm ...

  5. php xml 互相转换

    正好昨天才做过类似的需求……几行代码就可以搞定. 如果你使用 curl 获取的 xml data$xml = simplexml_load_string($data);$data['tk'] = js ...

  6. 一个简单的XML与数组之间的转换

    xml是网络使用最多的数据交换格式,所以,不掌握怎么操作它,又有蛋疼的了. php中可以操作xml的类/函数很多,个人认为最简单的是SimpleXMLElement这个类,它的使用就跟其名字一样:简单 ...

  7. xml转array

    1.字串 $xml = simplexml_load_string($data);$array = json_decode(json_encode($xml),TRUE); 2.文件$xml = si ...

  8. PHP解析xml

    <?xml version="1.0" encoding="UTF-8"?> <ZIP_result> <result name= ...

  9. php操作xml详解

    XML是一种流行的半结构化文件格式,以一种类似数据库的格式存储数据.在实际应用中,一些简单的.安全性较低的数据往往使用 XML文件的格式进行存储.这样做的好处一方面可以通过减少与数据库的交互性操作提高 ...

随机推荐

  1. Centos部署nagios+apache实现服务器监控

    1.Nagios介绍 nagios是 一款功能强大的网络监视工具,它可以有效的监控windows.linux.unix主机状态以及路由器交换机的网络设置,打印机工作状态等,并将状态出 现异常的服务及时 ...

  2. [React Fundamentals] Component Lifecycle - Mounting Usage

    The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson ...

  3. android122 zhihuibeijing 主页面搭建

    右边主页面布局设计: 文字颜色选择器和是否点击的图片选择器  路径和写法: <?xml version="1.0" encoding="utf-8"?&g ...

  4. 动态追踪技术(中) - Dtrace、SystemTap、火焰图

    http://openresty.org/cn/presentations.html http://weibo.com/agentzh?is_all=1 http://openresty.org/po ...

  5. 【面试题】如何让C语言自动发现泄漏的内存

    1. 题目 改造malloc和free函数,使C语言能自动发现泄漏的内存,在程序退出时打印中遗漏的内存地址和大小. 2. 思路 用一个链表来记录已经分配的内存地址.在malloc时,把分配的内存地址和 ...

  6. Java ZIP File Example---refernce

    In this tutorial we are going to see how to ZIP a file in Java. ZIP is an archive file format that e ...

  7. php安装ecshop

    1.apache2.2正常 2.源码htdoc下面保存不动 3.E:\PHP 安装目录下 php5.3.28 就不支持jpeg php5.2.17 就不支持 mysql 说明就是php的问题,因为其他 ...

  8. android学习——popupWindow 在指定位置上的显示

    先看效果图,免得浪费大家时间,看是不是想要的效果 . 直接上代码 ,核心方法. [java] view plaincopy private void showPopupWindow(View pare ...

  9. About gpref O(n2) --> O(1)

    http://www.ibm.com/developerworks/cn/linux/l-gperf.html 命令行处理和 gperf 的作用 命令行处理一直以来都是软件开发中最容易被忽视的领域.几 ...

  10. 命令行界面下用户和组管理之groupmod和groupdel的使用

    NAME    groupmod - modify a group definition on the system SYNOPSIS       groupmod [options] GROUP O ...