我正在尝试使用PHP DOM将数据插入到我的RSS xml文件中。 这些是涉及的代码。
的index.php
<html>
<head><title>test</title></head>
</head>
<form name="input" action="insert.php" method="post">
insert item:
<input type="text" name="item"/>
<input type="submit" value="send"/>
</form>
</body>
</html>
insert.php
<?php
header('Location:index.php');
$xmldoc = new DOMDocument();
$xmldoc->load('sample.xml');
$newAct = $_POST['activity'];
$root = $xmldoc->getElementsByTagName('channel')->item(0);
$newElement = $xmldoc->createElement('item');
$root->appendChild($newElement);
$newText = $xmldoc->createTextNode($newAct);
$newElement->appendChild($newText);
$xmldoc->save('sample.xml');
?>
sample.xml中
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Bagnista Home Page</title>
<link>example.com</link>
<description>Bagnista Home Page</description>
<atom:link href="example.xml" rel="self" type="application/rss+xml"/>
<item>
<title>Kelly 28 in Fuschia Pink</title>
<link>example.com</link>
<language>en</language>
<pubDate>Wed, 04 Dec 2013 00:00:00 GMT</pubDate>
<description>For rent. Price $10000</description>
<guid isPermaLink="false">example.com</guid>
</item>
<item>
<title>Birkin 30 in RougeVif GHW</title>
<link>example.com</link>
<language>en</language>
<pubDate>Wed, 04 Dec 2013 00:00:00 GMT</pubDate>
<description>For rent. Price $13000</description>
<guid isPermaLink="false">example.com</guid>
</item>
<item>
<title>Birkin 30, gold hardware</title>
<link>example.com</link>
<language>en</language>
<pubDate>Wed, 04 Dec 2013 00:00:0 GMT</pubDate>
<description>For sale. Price $14000</description>
<guid isPermaLink="false">example.com</guid>
</item>
</channel>
</rss>
由于我使用的是appendChild,因此item标记将位于last / item标记的下方。 如何在第一个商品标签之前插入商品标签? 我已经尝试过insertBefore()但是它失败了。