首页 技术 正文
技术 2022年11月9日
0 收藏 599 点赞 3,403 浏览 1273 个字

PHP XML文件编程

一、PHP DOM编程

<?php
//1.创建dom对象
$xmldoc=new DOMDocument();
//2.加载xml(指定对哪个xml文件进行操作)
$xmldoc->load("2.xml");//在内存中形成dom树
//小技巧 希望知道xml 有哪些方法或者属性可以用。 最简单的方法 使用var_dump()
$stus=$xmldoc->getElementsByTagName("学生");
echo "共有".$stus->length;
//选择第一个学生
$stu1=$stus->item(0);
$stu_name=$stu1->getElementsByTagName("名字");
echo $stu_name->item(0)->nodeValue."<br>";?>

dom元素的获得和修改

<?php
$xmlDom=new DOMDocument();
$xmlDom->load("2.xml");
//取出根节点
$root=$xmlDom->getElementsByTagName("班级")->item(0);
//创建学生节点
$stu_node=$xmlDom->createElement("学生");
//创建名字节点
$stu_node_name=$xmlDom->createElement("名字");
$stu_node_name->nodeValue="小王";
//创建年龄节点
$stu_node_age=$xmlDom->createElement("年龄");
$stu_node_age->nodeValue="25";
//创建介绍节点
$stu_node_info=$xmlDom->createElement("介绍");
$stu_node_info->nodeValue="哈哈您好";
//建立连接
$stu_node->appendChild($stu_node_name);
$stu_node->appendChild($stu_node_age);
$stu_node->appendChild($stu_node_info);
//把创建的节点挂在根节点上
$root->appendChild($stu_node);
//重新保存为xml
//如果save是原文件 是更新, 如果是新的文件名 是创建新的文件
$xmlDom->save("2.xml");
?>

元素的删除

<?php
//xml文件的删除
$xmlDom=new DOMDocument();
$xmlDom->load("2.xml");
$root=$xmlDom->getElementsByTagName("班级")->item(0);
//找到这个学生
$stus=$xmlDom->getElementsByTagName("学生");
$stu=$stus->item(1);
$stu->parentNode->removeChild($stu);
//$root->removeChild($stu);
$xmlDom->save("2.xml");?>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905