php xml、删除 修改 创建 增加
分别是创建、增加、删除、修改四个功能,变量都是写死的,改一改用$_post方式接收就可以用了
//index.php教程 创建功能
复制代码 代码如下:
<?php
$xmlpatch = 'index.xml';
$_id = '1';
$_title = 'title1';
$_content = 'content1';
$_author = 'author1';
$_sendtime = 'time1';
$_htmlpatch = '1.html';
111cn.net$doc = new domdocument('1.0', 'utf-8');
$doc -> formatoutput = true;
111cn.net$root = $doc -> createelement('root');//新建节点
111cn.net$index = $doc -> createelement('index');//新建节点
111cn.net$url = $doc -> createattribute('url');//新建属性
$patch = $doc -> createtextnode($_htmlpatch);//新建text值
$url -> appendchild($patch);//将$patch文本设为$url属性的值
111cn.net$id = $doc -> createattribute('id');
$newsid = $doc -> createtextnode($_id);
$id -> appendchild($newsid);
111cn.net$title = $doc -> createattribute('title');
$newstitle = $doc -> createtextnode($_title);
$title -> appendchild($newstitle);
111cn.net$content = $doc -> createtextnode($_content);//节点值
111cn.net$author = $doc -> createattribute('author');
$newsauthor = $doc -> createtextnode($_author);
$author -> appendchild($newsauthor);
111cn.net$sendtime = $doc -> createattribute('time');
$newssendtime = $doc -> createtextnode($_sendtime);
$sendtime -> appendchild($newssendtime);
111cn.net$index -> appendchild($id);//将$id设为index节点的属性,以下类同
$index -> appendchild($title);
$index -> appendchild($content);
$index -> appendchild($url);
$index -> appendchild($author);
$index -> appendchild($sendtime);
111cn.net$root -> appendchild($index);//设置index为root字节点
111cn.net$doc -> appendchild($root);//设置root为跟节点
111cn.net$doc -> save($xmlpatch);//保存文件
111cn.netecho $xmlpatch . ' has create success';
111cn.net?>
111cn.net<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>xml操作</title>
</head>
111cn.net<body>
</body>
</html>
//add.php 增加功能(跟index.php文件差不多,主要就是加个load载入跟 $root = $doc -> documentelement获得跟节点
复制代码 代码如下:
<?php
$xmlpatch = 'index.xml';
$_id = '2';
$_title = 'title2';
$_content = 'content2';
$_author = 'author2';
$_sendtime = 'time2';
$_htmlpatch = '2.html';
111cn.net$doc = new domdocument();
$doc -> formatoutput = true;
if($doc -> load($xmlpatch)) {
$root = $doc -> documentelement;//获得根节点(root)
$index = $doc -> createelement('index');
111cn.net$url = $doc -> createattribute('url');
$patch = $doc -> createtextnode($_htmlpatch);
$url -> appendchild($patch);
111cn.net$id = $doc -> createattribute('id');
$newsid = $doc -> createtextnode($_id);
$id -> appendchild($newsid);
111cn.net$title = $doc -> createattribute('title');
$newstitle = $doc -> createtextnode($_title);
$title -> appendchild($newstitle);
111cn.net$content = $doc -> createtextnode($_content);
111cn.net$author = $doc -> createattribute('author');
$newsauthor = $doc -> createtextnode($_author);
$author -> appendchild($newsauthor);
111cn.net$sendtime = $doc -> createattribute('time');
$newssendtime = $doc -> createtextnode($_sendtime);
$sendtime -> appendchild($newssendtime);
111cn.net$index -> appendchild($id);
$index -> appendchild($title);
$index -> appendchild($content);
$index -> appendchild($url);
$index -> appendchild($author);
$index -> appendchild($sendtime);
111cn.net$root -> appendchild($index);
111cn.net$doc -> save($xmlpatch);
111cn.netecho $_id . ' has been added in ' . $xmlpatch;
111cn.net} else {
echo 'xml file loaded error!';
}
?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>xml操作-添加</title>
</head>
111cn.net<body>
</body>
</html>
//edit.php 修改功能(这里只修改title属性值 跟节点值)
复制代码 代码如下:
<?php
$xmlpatch = 'index.xml';
$_id = '2';
$_title = 'has been changed';
$_content = 'has been changed';
111cn.net$doc = new domdocument();
$doc -> formatoutput = true;
111cn.netif($doc -> load($xmlpatch)) {
$root = $doc -> documentelement;
$elm = $root -> getelementsbytagname('index');
$checkexist = 0;
foreach ($elm as $new) {
if($new -> getattribute('id') == $_id) {
$new -> setattribute('title', $_title);
$new -> nodevalue = $_content;//修改节点值,真是太意外了,没想到跟js一样直接能赋值...
//$new -> removechild($new -> nodevalue);
$checkexist = 1;
}
}
if($checkexist == 0) {
echo $_id . ' is not found in ' . $xmlpatch;
} else {
$doc -> save($xmlpatch);
echo $_id . ' has been changed';
}
} else {
echo 'xml file loaded error!';
}
111cn.net?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>xml操作-修改</title>
</head>
111cn.net<body>
</body>
</html>
//del.php 删除功能
复制代码 代码如下:
<?php
$xmlpatch = 'index.xml';
$_id = '2';
111cn.net$doc = new domdocument();
$doc -> formatoutput = true;
if($doc -> load($xmlpatch)) {
$root = $doc -> documentelement;
$elm = $root -> getelementsbytagname('index');
foreach ($elm as $new) {
if($new -> getattribute('id') == $_id) {
if($root -> removechild($new)) {
echo $_id . ' has been deleted';
} else {
echo $_id . ' delete failed';
}
}
}
$doc -> save($xmlpatch);
} else {
echo 'xml file loaded error!';
}
111cn.net?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>xml操作-删除</title>
</head>
111cn.net<body>
</body>
</html>
xml 解析器。
语法: int xml_parser_create(string [encoding]);
返回值: 整数
函数种类: 资料处理
内容说明
本函数用来初始化一个新的 xml 解析器。参数 encoding 可省略,为 xml 使用的字符集,默认值为 iso-8859-1,其它尚有 us-ascii、utf-8 二种。成功则返回 parser 代码供其它函数使用,失败则返回 false 值。
*/
$xmlfile='test.xml'; //定义一个xml文件
$xmlparser=xml_parser_create(); //建立一个xml解析器
$fp=fopen($xmlfile,'r'); //打开一个文件并读取数据
while($xmldata=fread($fp,4096)) //循环读取文件内容
{
if(!xml_parse($xmlparser,$xmldata,feof($fp))) //解析xml数据,如果失败输出错误信息
{
die(print "error:".
xml_error_string(xml_get_error_code($xmlparser))."<br/>"."line:".
xml_get_current_line_number($xmlparser)."<br/>"."column:".
xml_get_current_column_number($xmlparser)."<br/>");
}
}
xml_parser_free($xmlparser); //释放xml解析器
/*
php 5 开始,自动侦测输入的 xml 的编码,因此 encoding 参数仅用来指定解析后输出数据的编码
在 php 5.0.0 和 5.0.1 总,默认输出的字符编码是 iso-8859-1,而 php 5.0.2 及以上版本是 utf-8。解析器支持的编码有 iso-8859-1, utf-8 和 us-ascii
bool xml_parser_free ( resource parser )
parser
指向要释放的 xml 解析器的指针。
如果 parser 参数没有指向一个合法的解析器,该函数将返回 false,否则将释放指定的解析器并返回 true
*/
$simple="<para><note>simple note</note></para>"; //定义xml数据
$p=xml_parser_create(); //创建xml解析器
xml_parse_into_struct($p,$simple,$vals,$index); //将数据解析到数组
xml_parser_free($p); //释放xml解析器
echo "index arrayn";
print_r($index); //输出结果数组
echo "nvals arrayn";
print_r($vals);
/*输出结果为:
index array
array
(
[para] => array
(
[0] => 0
[1] => 2
)
[note] => array
(
[0] => 1
)
)
vals array
array
(
[0] => array
(
[tag] => para
[type] => open
[level] => 1
)
[1] => array
(
[tag] => note
[type] => complete
[level] => 2
[value] => simple note
)
[2] => array
(
[tag] => para
[type] => close
[level] => 1
)
)
*/
xml文档格式如下
<?xml version="1.0" encoding="utf-8"?>
<list>
<company>武汉xxx公司</company>
<user>
<name>张三</name>
<age sex="未知">a</age>
<height>1</height>
</user>
<user>
<name>李四</name>
<age sex="女">b</age>
<height>2</height>
</user>
<user>
<name>王五</name>
<age sex="男">c</age>
<height>3</height>
</user>
<town parent="0" id="1">台北</town>
<town parent="1" id="2">板桥</town>
<town parent="0" id="3">桃园</town>
</list>
php解析代码
*/
header("content-type:text/html; charset=utf-8"); //设置编码
$xml = simplexml_load_file('a.xml'); //载入xml文件 $lists和xml文件的根节点是一样的
echo $xml->company."<br>";
echo $xml->town."<br>id:";
echo $xml->town['id']."<br>parent:";
echo $xml->town['parent']."<br>";
echo "<br>循环读取:<br>";
foreach($xml->user as $users){ //有多个user,取得的是数组,循环输出
echo "-------------------<br>";
echo "姓名:".$users->name."<br>";
echo "编号:".$users->age."<br>";
echo "性别:".$users->age['sex']."<br>";
echo "序号:".$users->height."<br>";
}
echo "<br>循环读取:<br>";
foreach($xml->town as $towns){ //有多个user,取得的是数组,循环输出
echo "-------------------<br>";
echo "id:".$towns['id']."<br>";
echo "归属:".$towns['parent']."<br>";
echo "地区:".$towns."<br>";
}
/*
定义和用法
simplexml_load_file() 函数把 xml 文档载入对象中。
如果失败,则返回 false。
语法
simplexml_load_file(file,class,options,ns,is_prefix)参数 描述
file 必需。规定要使用的 xml 文档。
class 可选。规定新对象的 class。
options 可选。规定附加的 libxml 参数。
ns 可选。
is_prefix 可选。
返回值
返回类 simplexmlelement 的一个对象,该对象的属性包含 xml 文档中的数据。如果失败,则返回 false
代码如下 | 复制代码 |
$sql = "查询数据库文件"; echo "<photo desc='$result[文件名字段]' url='_pics/$result[文件名字段]' />"; } function _delimage($path){ //利用domdocument ?> |
//-------------------------------------------------------------------------------------
代码如下 | 复制代码 |
function loadfile($file){ |
相关文章
- 下面小编来给大家演示几个php操作zip文件的实例,我们可以读取zip包中指定文件与删除zip包中指定文件,下面来给大这介绍一下。 从zip压缩文件中提取文件 代...2016-11-25
- 用PS将人像脸部打造成打散颗粒的效果,成品出来后很美,过程略复杂,需要耐心和细心。现在跟着步骤来做吧! 1、首先在PS里面打开这张图。 2、选择快速选择工具的增加...2016-12-21
- 复制代码 代码如下: <td> <a href="/member/life/edit_ppt/<?php echo $v->id;?>" class="btn">编辑</a> <a href="javascript:;" onclick="if(confirm('您确定删除这条记录?')){location.href='/member/life/d...2014-06-07
- 一般在线增加从库有两种方式,一种是通过mysqldump备份主库,恢复到从库,mysqldump是逻辑备份,数据量大时,备份速度会很慢,锁表的时间也会很长。另一种是通过xtrabackup工具备份主库,恢复到从库,xtrabackup是物理备份,备份速度快...2015-11-08
- 伪造跨站请求介绍伪造跨站请求比较难以防范,而且危害巨大,攻击者可以通过这种方式恶作剧,发spam信息,删除数据等等。...2013-10-01
- 1.创建div元素: Javascript代码 复制代码 代码如下: <scripttypescripttype="text/javascript"> functioncreateElement(){ varcreateDiv=document.createElement("div"); createDiv.innerHTML="Testcreateadiveleme...2013-10-13
- 这篇文章主要介绍了JS创建Tag标签的方法,结合具体实例形式分析了javascript动态操作页面HTML元素实现tag标签功能的步骤与相关操作技巧,需要的朋友可以参考下...2017-06-15
Centos中彻底删除Mysql(rpm、yum安装的情况)
我用的centos6,mysql让我整出了各种问题,我想重装一个全新的mysql,yum remove mysql-server mysql之后再install并不能得到一个干净的mysql,原来的/etc/my.cnf依然没变,datadir里面的数据已没有任何变化,手动删除/etc/my.cn...2015-03-15- 数据库中的数据删除会分为两种:物理删除 和 逻辑删除,接下来通过本文给大家介绍MyBatis-Plus的物理删除和逻辑删除使用场景分析,感兴趣的朋友一起看看吧...2021-09-25
- 最近由于项目的需要,需要动态的添加和删除table中的tr,感觉用JS可以实现,但是在网上找了一下,单纯的自己写JS,感觉太麻烦,而且也不好维护。于是想到了最近学的jQuery。这篇文章给大家用实例介绍了jQuery动态添加与删除tr行的方法,有需要的朋友们可以参考借鉴。...2016-10-20
- 这篇文章主要介绍了mybatis-plus getOne和逻辑删除,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-08-26
- 这篇文章主要介绍了C# 复制与删除文件的实现方法的相关资料,希望通过本文能帮助到大家,让大家理解掌握这部分内容,需要的朋友可以参考下...2020-06-25
- 这篇文章主要为大家详细介绍了js实现上传文件添加和删除文件选择框 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-25
- 这篇文章主要介绍了C#删除UL LI中指定标签里文字的方法,涉及C#针对页面HTML元素进行正则匹配与替换的相关操作技巧,需要的朋友可以参考下...2020-06-25
- PS怎么创建变形文字?ps中想要给输入的文字变形,该怎么调整文字的显示形态呢?下面我们就来看看ps给文字变形的方法,需要的朋友可以参考下 我们在图层上输入文字后,可以...2017-07-06
- 什么是cookie? cookie 是存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie。你可以使用 JavaScript 来创建和取回 cookie 的值。 有关cookie的例子: 名字 cookie 当访...2014-05-31
- 这篇文章主要介绍了MybatisPlus实现逻辑删除功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-25
- 这篇文章主要介绍了C++递归删除一个目录的实现方法,涉及到目录的操作及递归算法的应用,需要的朋友可以参考下...2020-04-25
JQuery EasyUI学习教程之datagrid 添加、修改、删除操作
这篇文章主要介绍了JQuery EasyUI datagrid 添加、修改、删除操作的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-07-25- 这篇文章主要介绍了jQuery中DOM节点的删除方法,文中介绍的很相信,内容包括empty()的基本用法、remove()的有参用法和无参用法、empty和remove区别、保留数据的删除操作detach()以及detach()和remove()区别,需要的朋友可以参考借鉴。...2017-01-26