Php – DomDocument failing to add a “link” element for RSS feed


I am trying to create an RSS feed in PHP using DomDocument but every time I try to make a node like the script fails

$oDomDocument = new DOMDocument( "1.0", "iso-8859-1" );

// Create the root now
$oRootNode = $oDomDocument->createElement( "rss" );
$oRootNode->setAttribute( "version", "2.0" );
$oDomDocument->appendChild( $oRootNode );

// Create the channel node
$oChannelNode = $oDomDocument->createElement( "channel" );
$oRootNode->appendChild( $oChannelNode );

// Add site details
$oChannelNode->appendChild( $oDomDocument->createElement( "title", "Site Title" ) );
$oChannelNode->appendChild( $oDomDocument->createElement( "link", "" ) );
$oChannelNode->appendChild( $oDomDocument->createElement( "description", "This is a description" ) );
$oChannelNode->appendChild( $oDomDocument->createElement( "language", "en-us" ) );

I get the following error..

XML Parsing Error: mismatched tag. Expected: .
Location: http://daddydonkey/feed.rss
Line Number 4, Column 58:This is a description

Best Solution

I tried your PHP code and it works.

Related Question