我是靠谱客的博主 留胡子小蝴蝶,这篇文章主要介绍php操作XML、读取数据和写入数据的实现代码,现在分享给大家,希望可以做个参考。

xml文件

复制代码
1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?> <vip> <id>23</id> <username>开心的路飞</username> <sex>男</sex> <face>face/43.jpg</face> <email>123@qq.com</email> <qq>1212121212</qq> </vip>
登录后复制


php解析XML获取标签中的值

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* * _get_xml 获取的XML文件 * @access public 表示函数对外公开 * @param $_xmlfile xml文件 * $_html 从XML中取出的数据数组 * */ function _get_xml($_xmlfile){ $_html = array(); if(file_exists($_xmlfile)){ $_xml = file_get_contents($_xmlfile); preg_match_all('/<vip>(.*)</vip>/', $_xml,$_dom); foreach($_dom[1] as $_value){ preg_match_all('/<id>(.*)</id>/', $_value,$_id); preg_match_all('/<username>(.*)</username>/', $_value,$_username); preg_match_all('/<sex>(.*)</sex>/', $_value,$_sex); preg_match_all('/<face>(.*)</face>/', $_value,$_face); preg_match_all('/<email>(.*)</email>/', $_value,$_email); preg_match_all('/<qq>(.*)</qq>/', $_value,$_qq); $_html['id'] = $_id[1][0]; $_html['username'] = $_username[1][0]; $_html['sex'] = $_sex[1][0]; $_html['face'] = $_face[1][0]; $_html['email'] = $_email[1][0]; $_html['qq'] = $_qq[1][0]; } }else{ _alert_back("文件不存在"); } return $_html; }
登录后复制

php向XML文件中写入数据

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* * _set_xml将信息写入XML文件 * @access public 表示函数对外公开 * @param $_xmlfile xml文件 * @param $_clean 要写入的信息的数组 * */ function _set_xml($_xmlfile,$_clean){ $_fp = @fopen('newuser.xml','w'); if(!$_fp){ exit('系统错误,文件不存在!'); } flock($_fp,LOCK_EX); $_string = "<?xml version="1.0" encoding="utf-8"?>rt"; fwrite($_fp, $_string,strlen($_string)); $_string = "<vip>rt"; fwrite($_fp, $_string,strlen($_string)); $_string = "t<id>{$_clean['id']}</id>rt"; fwrite($_fp, $_string,strlen($_string)); $_string = "t<username>{$_clean['username']}</username>rt"; fwrite($_fp, $_string,strlen($_string)); $_string = "t<sex>{$_clean['sex']}</sex>rt"; fwrite($_fp, $_string,strlen($_string)); $_string = "t<face>{$_clean['face']}</face>rt"; fwrite($_fp, $_string,strlen($_string)); $_string = "t<email>{$_clean['email']}</email>rt"; fwrite($_fp, $_string,strlen($_string)); $_string = "t<qq>{$_clean['url']}</qq>rt"; fwrite($_fp, $_string,strlen($_string)); $_string = "</vip>"; fwrite($_fp, $_string,strlen($_string)); flock($_fp,LOCK_UN); fclose($_fp); }
登录后复制

立即学习
全程直播 + 实战授课 + 边学 + 边练 + 边辅导

最后

以上就是留胡子小蝴蝶最近收集整理的关于php操作XML、读取数据和写入数据的实现代码的全部内容,更多相关php操作XML、读取数据和写入数据内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(83)

评论列表共有 0 条评论

立即
投稿
返回
顶部