• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php 读写 xml

php 搞代码 3年前 (2022-01-23) 19次浏览 已收录 0个评论

读取xml信息

test.xml

<?xml version="1.0" encoding="UTF-8"?>			张三		23		努力学习				李四		23		努力学习	

test.php

<?php	$xmldoc = new DOMDocument();		$xmldoc->load("test.xml");		$stus = $xmldoc->getElementsByTagName("student");		for($i=0;$i length;$i++){				$stu = $stus->item($i);		echo getNodeVal($stu,"name")."--".getNodeVal($stu,"age")."--".				getNodeVal($stu,"intro")."
"; } function getNodeVal(&$MyNode,$tagName){ return $MyNode->getElementsByTagName($tagName)->item(0)->nodeValue; }?>

写xml:

<?php	$xmldoc = new DOMDocument("1.0","utf-8");		$xmldoc->load("classes2.xml");		//添加一个学生信息	//创建学生结点	$root = $xmldoc->getElementsByTagName("class")->item(0);	$stu_node = $xmldoc->createElement("student");		$stu_node->setAttribute("sex", "男");		//创建名字结点并且挂载到学生结点下	$stu_node_name=$xmldoc->createElement("name");	$stu_node_name->nodeValue = "唐唐";	$stu_node->appendChild($stu_node_name);		$stu_node_age=$xmldoc->createElement("age");	$stu_node_age->nodeValue = "24";	$stu_node->appendChild($stu_node_age);		$stu_node_intro=$xmldoc->createElement("intro");	$stu_node_intro->nodeValue = "我是唐唐,我一直很努力";	$stu_node->appendChild($stu_node_intro);		//把学生结点挂载到根结点下	$root->appendChild($stu_node);		$xmldoc->save("new.x本¥文来源gaodai$ma#com搞$代*码*网(搞代gaodaima码ml");?>

删除结点:

<?php	$xmldoc = new DOMDocument("1.0","utf-8");		$xmldoc->load("test.xml");	//删除一个结点	$root = $xmldoc->getElementsByTagName("class")->item(0);		$stus = $xmldoc->getElementsByTagName("student");		$stu1 = $stus->item(2);		//$root->removeChild($stu1);		$stu1->parentNode->removeChild($stu1);		$xmldoc->save("test.xml");?>

修改结点:

<?php	$xmldoc = new DOMDocument("1.0","utf-8");		$xmldoc->load("test.xml");	//删除一个结点	$root = $xmldoc->getElementsByTagName("class")->item(0);		$stus = $xmldoc->getElementsByTagName("student");		$stu1 = $stus->item(0);		$stu1_name = $stu1->getElementsByTagName("name")->item(0);	$stu1_name->nodeValue = "唐唐改名了";		$xmldoc->save("test.xml");?>

以上就介绍了php 读写 xml,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 读写 xml

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址