发布于 2017-08-16 11:55:42 | 146 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的PHP设计模式,程序狗速度看过来!

PHP开源脚本语言

PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于Web开发领域。PHP的文件后缀名为php。


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

本文实例讲述了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程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务