【问题标题】:Set new xml data before old xml data在旧 xml 数据之前设置新的 xml 数据
【发布时间】:2016-03-31 15:45:53
【问题描述】:
    <?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>

如果你能帮我编写这个代码真的需要它,所以它不会以错误的方式打印出提要

【问题讨论】:

  • 如果你把echo gettype($xml);die();放在第13行,它输出object吗?
  • 是的,它剂量输出对象
  • 这可能很有用,stackoverflow.com/a/30582620

标签: php xml dom rss


【解决方案1】:

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

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

RSS 提要的根标签(文档元素)应该是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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2011-10-03
    • 1970-01-01
    相关资源
    最近更新 更多