PHP实现对xml的增删改查操作案例分析

这篇文章主要介绍了PHP实现对xml的增删改查操作,结合具体案例形式分析了php针对xml格式文件的增删改查操作相关实现技巧,需要的朋友可以参考下

本文实例讲述了PHP实现对xml的增删改查操作。分享给大家供大家参考,具体如下:

案例:

index.php

 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."
"; } //添加一个学生信息 //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

  <班级>

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具:
http://tools.0133.cn/code/xmljson

在线格式化XML/在线压缩XML
http://tools.0133.cn/code/xmlformat

XML在线压缩/格式化工具:
http://tools.0133.cn/code/xml_format_compress

XML代码在线格式化美化工具:
http://tools.0133.cn/code/xmlcodeformat

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP针对XML文件操作技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

以上就是PHP实现对xml的增删改查操作案例分析的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » PHP编程