Question

I have a PHP function (written by a stackoverflow member) that is supposed to write values into an XML file. One of the elements of the XML needs to be contained in a CDATA. Specifically the "description" value. When I run the PHP code, I get the following error:

Warning: DOMDocument::createElement() expects parameter 2 to be string, object given in /homepages/46/d412048482/htdocs/create_feed.php on line 23

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

I'm not sure where to go from here. Does anyone have any suggestions?

Full 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 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>
Was it helpful?

Solution

createCDATASection() creates an XML cdata node, createElement() or createTextNode() create other node types.

You need to append it to your description element node:

$description = $newItem->appendChild($xml->createElement('description'));
$description->appendChild($xml->createCDATASection($_POST['description']));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top