用php实现xml的写入追加

xml未追加前格式:
<?xml version="1.0" encoding="utf-8"?>
<page>
  <article>
    <title>博客 </title>
    <author>11</author>
    <body>这里是内容11</body>
    <tag>222 </tag>
    <time>2017年03月17日</time>
    <view>520 </view>
  </article>
</page>

追加后的内容为

<?xml version="1.0" encoding="utf-8"?>
<page>
  <article>
    <title>博客 </title>
    <author>11</author>
    <body>这里是内容11</body>
    <tag>222 </tag>
    <time>2017年03月17日</time>
    <view>520 </view>
  </article>

  <article>
    <title>博客 </title>
    <author>11</author>
    <body>这里是内容11</body>
    <tag>222 </tag>
    <time>2017年03月17日</time>
    <view>520 </view>
  </article>

</page>

PHP代码为:

public function CreateXml($title_value,$tags,$author_value,$body_value,$time_value,$view_num){     
			$xmlpath = "./xml/info.xml";
			$dom = new DomDocument('1.0','utf-8');
			$dom->formatOutput = true;
			// $dom->load($xmlpath);
			// $tag = $dom->getElementsByTagName("title");
			if (file_exists($xmlpath)) {
				# 如果文件存在,则进行追加
				
				$dom->formatOutput = true;
				$dom->load($xmlpath);
				$newarticles = $dom->createElement('article');
				$articles = $dom->getElementsByTagName("page")->item(0);  //找到文件追加的位置
				$articles->appendChild($newarticles);				//进行文件追加

				$title = $dom->createElement('title');
				$title->appendChild($dom->createTextNode($title_value));
				$newarticles->appendChild($title);

				$author = $dom->createElement('author');
				$author->appendChild($dom->createTextNode($author_value));
				$newarticles->appendChild($author);

				$body = $dom->createElement('body');
				$body->appendChild($dom->createTextNode($body_value));
				$newarticles->appendChild($body);

				$tag = $dom->createElement('tag');
				$tag->appendChild($dom->createTextNode($tags));
				$newarticles->appendChild($tag);

				$time = $dom->createElement('time');
				$time->appendChild($dom->createTextNode($time_value));
				$newarticles->appendChild($time);

				$view = $dom->createElement('view');
				$view->appendChild($dom->createTextNode($view_num));
				$newarticles->appendChild($view);

				$dom->save($xmlpath);
			}
			else{
				#如果文件不存在,则进行文件写入
				//$dom = new DomDocument('1.0','utf-8');
				$dom->formatOutput = true;
				

				$page = $dom->createElement('page');
				$dom->appendChild($page);

				$articles = $dom->createElement('article');
				$page->appendChild($articles);

				$title = $dom->createElement('title');
				$title->appendChild($dom->createTextNode($title_value));
				$articles->appendChild($title);

				$author = $dom->createElement('author');
				$author->appendChild($dom->createTextNode($author_value));
				$articles->appendChild($author);

				$body = $dom->createElement('body');
				$body->appendChild($dom->createTextNode($body_value));
				$articles->appendChild($body);

				$tag = $dom->createElement('tag');
				$tag->appendChild($dom->createTextNode($tags));
				$articles->appendChild($tag);

				$time = $dom->createElement('time');
				$time->appendChild($dom->createTextNode($time_value));
				$articles->appendChild($time);

				$view = $dom->createElement('view');
				$view->appendChild($dom->createTextNode($view_num));
				$articles->appendChild($view);

				$dom->save($xmlpath);
			}
		}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值