基于PHP对XML的操作详解

编程学习 2021-07-05 09:49www.dzhlxh.cn编程入门
本篇文章是关于PHP对XML的操作进行了详细的分析介绍,需要的朋友参考下

<?php 

    $xml = simplexml_load_file('example.xml');              //创建SimpleXML对象  
    var_dump($xml);                                                             //输出XML  
?> 

<?php 
    $xml = simplexml_load_file('example.xml');          //读取XML文件  
    foreach($xml->depart as $a)                                    //循环读取XML数据中的每一个depart标签  
    { 
        echo "$a->name <BR>";                                       //输出其中的name属性  
    } 
?> 

<?php 
    $xml = simplexml_load_file('example.xml');          //读取XML文件  
    echo $xml->depart->name[0];                                  //输出节点  
?> 

<?php 
    $xml = simplexml_load_file('example.xml'); 
    foreach ($xml->depart->children() as $depart)             //循环读取depart标签下的子标签  
    { 
       var_dump($depart);                                                        //输出标签的XML数据  
    } 
?> 

<?php 
    $xml = simplexml_load_file('example.xml');                      //读取XML文件  
    $result = $xml->xpath('/departs/depart/employees/employee/name');        //定义节点  
    var_dump($result);                                          //输出节点  
?> 

<?php 
    $xml = simplexml_load_file('example.xml');              //读取XML  
    $xml->depart->name[0] = "Human Resource";         //修改节点  
?> 

<?php 
    $xml = simplexml_load_file('example.xml');              //读取XML数据  
    echo $xml->asXML();                                                     //标准化XML数据  
?> 

<?php 
    $xml = simplexml_load_file('example.xml');              //读取XML数据  
    $newxml = $xml->asXML();                         //标准化XML数据  
    $fp = fopen("newxml.xml", "w");                  //打开要写入XML数据的文件  
    fwrite($fp, $newxml);                                    //写入XML数据  
    fclose($fp);                                                     //关闭文件  
?> 

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板