在旧的xml数据之前设置新的xml数据

时间:2016-03-31 15:45:54

标签: php xml dom rss

    <?php 
  if (isset($_POST['insert'])) {
    $xml = new DomDocument("1.0","UTF-8");
    $xml->load('rss.xml');
    $title = $_POST['title'];
    $image = $_POST['image'];
    $user = $_POST['user'];
    $date = date("Y/m/d");
    $indent = $_POST['indent'];
    $description = $_POST['description'];

    $rootTag = $xml->documentElement;

    $entryTag = $xml->createElement("entry");
    $titleTag = $xml->createElement("title", $title);
    $imageTag = $xml->createElement("image", $image);
    $userTag = $xml->createElement("user", $user);
    $dateTag = $xml->createElement("date", $date);
    $indentTag = $xml->createElement("indent", $indent);
    $descriptionTag = $xml->createElement("description", $description);

    $entryTag->appendChild($titleTag);
    $entryTag->appendChild($imageTag);
    $entryTag->appendChild($userTag);
    $entryTag->appendChild($dateTag);
    $entryTag->appendChild($indentTag);
    $entryTag->appendChild($descriptionTag);
    $rootTag->appendChild($entryTag);
    $xml->save('rss.xml');
  }
?>

我想在其他元素之前插入此内容,我可以得到帮助,这个编码工作

这是我想要它做的:

  

旧xml

<channel>
    <entry>
    <title>We Are Back</title>
    <image>assets/img/landscape-mountains-nature-clouds.jpg</image>
    <user>RKGaming Admin</user>
    <date>1/14/16</date>
    <indent>It's</indent>
    <description>
    our first post in the year 2016, sorry the website has been down we have been working on it trying to integrate a login system for the consumer base. We thank you for your time to wait and hope you enjoy the new updates coming from our company RKGaming in the near future. We still are looking for people to work with, so please join our ranks and sign up for the company so we can produce the game!
    </description>
    </entry>
</channel>
  

表单子目录后的新XML

<channel>
<entry>
<title>test</title>
<image>test</image>
<user>test</user>
<date>2016/03/31</date>
<indent>test</indent>
<description>test</description>
</entry>
<entry>
    <title>We Are Back</title>
    <image>assets/img/landscape-mountains-nature-clouds.jpg</image>
    <user>RKGaming Admin</user>
    <date>1/14/16</date>
    <indent>It's</indent>
    <description>
    our first post in the year 2016, sorry the website has been down we have been working on it trying to integrate a login system for the consumer base. We thank you for your time to wait and hope you enjoy the new updates coming from our company RKGaming in the near future. We still are looking for people to work with, so please join our ranks and sign up for the company so we can produce the game!
    </description>
    </entry>

</channel>

如果你可以帮我编写代码,那真的是需要的,所以dosnt以错误的方式打印出来

1 个答案:

答案 0 :(得分:0)

该方法称为DOMDocument::getElementsByTagName()。它返回DOMNodeList。要获取节点列表的第一个元素,请使用DOMNodeList::item()

$rootTag = $xml->getElementsByTagName("roo")->item(0);

RSS Feed的根标记(文档元素)应为rss,而不是roo。文档元素也可以在DOMDocument::$documentElement属性中使用:

$rootTag = $xml->documentElement;

此外DOMNode::appendChild()返回附加节点。这允许它将其与DOMDocument::create*方法结合使用。您不应该使用DOMDocument::createElement()的第二个参数。这是一个可能导致XML损坏的错误。创建和附加文本节点:

$document = new DomDocument("1.0","UTF-8");
$document->appendChild($document->createElement('rss'));

$title = 'A Title';
$image = 'foo.png';
$description = 'Some text';

$rootTag = $document->documentElement;

$entryTag = $rootTag->appendChild($document->createElement("entry"));
$entryTag
  ->appendChild($document->createElement("title"))
  ->appendChild($document->createTextNode($title));
$entryTag
  ->appendChild($document->createElement("image"))
  ->appendChild($document->createTextNode($image));
$entryTag
  ->appendChild($document->createElement("description"))
  ->appendChild($document->createTextNode($description));

$document->formatOutput = TRUE;
echo $document->saveXml();

输出:

<?xml version="1.0" encoding="UTF-8"?>
<rss>
  <entry>
    <title>A Title</title>
    <image>foo.png</image>
    <description>Some text</description>
  </entry>
</rss>