简体   繁体   English

使用DOMDocument在PHP中解析CDATA

[英]Parsing CDATA in PHP with DOMDocument

I have a PHP function (written by a stackoverflow member) that is supposed to write values into an XML file. 我有一个PHP函数(由stackoverflow成员编写),应该将值写入XML文件。 One of the elements of the XML needs to be contained in a CDATA. XML的元素之一需要包含在CDATA中。 Specifically the "description" value. 特别是“描述”值。 When I run the PHP code, I get the following error: 当我运行PHP代码时,出现以下错误:

Warning: DOMDocument::createElement() expects parameter 2 to be string, object given in /homepages/46/d412048482/htdocs/create_feed.php on line 23 警告:DOMDocument :: createElement()期望参数2为字符串,第23行在/homepages/46/d412048482/htdocs/create_feed.php中给出的对象

Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given in /homepages/46/d412048482/htdocs/create_feed.php on line 23 可捕获的致命错误:传递给DOMNode :: appendChild()的参数1必须是DOMNode的实例,在/homepages/46/d412048482/htdocs/create_feed.php的第23行中给出了null

I'm not sure where to go from here. 我不确定从这里去哪里。 Does anyone have any suggestions? 有没有人有什么建议?

Full PHP 完整的PHP

<?php

// Script by Fred Fletcher, Canada.

$title = $_POST['title'];
$description = $_POST['description'];

$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('rss/dakashmere.xml');

$element = $xml->getElementsByTagName('item')->item(0);

$pubDate = $element->getElementsByTagName('pubDate')->item(0);
$title = $element->getElementsByTagName('title')->item(0);
$description = $element->getElementsByTagName('description')->item(0);

$newItem = $xml->createElement('item');

$newItem->appendChild($xml->createElement('title', $_POST['title']));
$newItem->appendChild($xml->createElement('description', $xml->createCDATASection($_POST['description'])));
$newItem->appendChild($xml->createElement('pubDate', date("F j, Y, g:i a",time())));;

$xml->getElementsByTagName('channel')->item(0)->insertBefore($newItem, $element);

$xml->save('rss/dakashmere.xml');

echo "Data has been written.";

?>

This is what I want the XML to look like 这就是我希望XML看起来像的样子

<?xml version="1.0" encoding="UTF-8"?>
<channel>
  <item>
    <title>Birthday Bash!</title>
    <description><![CDATA[ May 3rd, we will be celebrating Team Cherokee's DJ Unique Birthday Bash at Soho Hookah Bar and Lounge.  The event will be hosted by Prince Po of Organized Confusion and Honey Dipp.  There will be special performances by BITTA Records own Da Kashmere and T Solid.  There will also be guest appearances from VH1's Teeny Barrino and Benzino!<br><br><img src="https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-ash3/t1.0-9/10177877_440291492772340_8234232488405005533_n.jpg
" width="50%"> ]]></description>
    <pubDate>Sat, 3 May 2014 09:00:00 -0500</pubDate>
  </item>
</channel>

createCDATASection() creates an XML cdata node, createElement() or createTextNode() create other node types. createCDATASection()创建XML cdata节点,createElement()或createTextNode()创建其他节点类型。

You need to append it to your description element node: 您需要将其附加到您的description元素节点:

$description = $newItem->appendChild($xml->createElement('description'));
$description->appendChild($xml->createCDATASection($_POST['description']));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM