温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP如何实现对xml的增删改查操作

发布时间:2021-06-22 14:40:46 来源:亿速云 阅读:196 作者:小新 栏目:开发技术

小编给大家分享一下PHP如何实现对xml的增删改查操作,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

具体如下:

案例:

index.php

<?php header("content-type:text/html;charset=utf-8"); $xmldom = new DOMDocument(); $xmldom->load("demo2.xml"); //查询学生信息 $stus = $xmldom->getElementsByTagName("学生"); for ($i=0;$i<$stus->length;$i++){   $stu = $stus->item($i);   getxmlnode($stu, "姓名");   getxmlnode($stu, "年龄");   getxmlnode($stu, "性别");   getxmlnode($stu, "介绍"); } function getxmlnode(&$stu,$tagname){   echo $stuname = $stu->getElementsByTagName($tagname)->item(0)->nodeValue."<br/>"; } //添加一个学生信息 //addxml($xmldom); function addxml($xmldom){   $root = $xmldom->getElementsByTagName("班级")->item(0);   $ostus = $xmldom->createElement_x_x("学生");   //添加属性   $ostus->setAttribute("恋爱状况","热恋中");   //$ostus->nodeValue="\r\n";   $root->a($ostus);   $ostu_name = $xmldom->createElement_x_x("姓名");   $ostus->a($ostu_name);   $ostu_name->nodeValue="小娜";   $ostu_sex = $xmldom->createElement_x_x("性别");   $ostus->a($ostu_sex);   $ostu_sex->nodeValue="女";   $ostu_age = $xmldom->createElement_x_x("年龄");   $ostus->a($ostu_age);   $ostu_age->nodeValue="23";   $ostu_intro = $xmldom->createElement_x_x("介绍");   $ostus->a($ostu_intro);   $ostu_intro->nodeValue="高一美女";   $xmldom->save("demo2.xml"); } //删除一个学生信息 //del_element($xmldom); function del_element($xmldom){   $dstus = $xmldom->getElementsByTagName("学生");   $laststu = $dstus->item($dstus->length-1);   $laststu->parentNode->removeChild($laststu); } //修改一个学生信息 //update_element($xmldom); function update_element($xmldom){   $ustus = $xmldom->getElementsByTagName("学生");   $ustu = $ustus->item(0);   $ustu_age = $ustu->getElementsByTagName("年龄")->item(0);   $ustu_age->nodeValue+=10; } //写会到文件中 $xmldom->save("demo2.xml"); ?>

demo2.xml

<?xml version="1.0" encoding="UTF-8"?> <班级> </班级>

看完了这篇文章,相信你对“PHP如何实现对xml的增删改查操作”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI