V Posted July 3, 2010 Share Posted July 3, 2010 I wasn't anticipating to tackle so many languages in my first php project I made a script that creates XML files of all the posts within a category. (I plan to use it for RSS feeds) It's works fine but how do I add <rss version="2.0"> on top and an ending </rss> tag? I tried echoing it but I get errors. Also where would I add htmlentities ? I know RSS feeds don't use special characters. The code I use is require_once("functions.php"); $connection = dbConnect(); //connects to db $category = (int)$_GET['cat']; $sql = "SELECT * FROM posts WHERE cat_id = $category"; $result = $connection->query($sql) or die(mysqli_error($connection)); $doc = new DOMDocument(); $doc->formatOutput = true; $r = $doc->createElement( "channel" ); $doc->appendChild( $r ); while ($row = $result->fetch_assoc()) { //remember to use htmlentities $post_title = $row['post_title']; $post_content = $row['post_content']; $b = $doc->createElement( "item" ); $title = $doc->createElement( "title" ); $title->appendChild( $doc->createTextNode( $post_title ) ); $b->appendChild( $title ); $description = $doc->createElement( "description" ); $description->appendChild( $doc->createTextNode( $post_content ) ); $b->appendChild( $description ); $r->appendChild( $b ); } echo $doc->saveXML(); $doc->save("rss/" .$category. ".xml") :-\ and the generated XML files looks like this <?xml version="1"?> <channel> <item> <title>Some Post Title</title> <description>Post content..</description> </item> <item> <title>Another Title</title> <description>Post content..</description> </item> </channel> Quote Link to comment https://forums.phpfreaks.com/topic/206658-xml-createelement/ Share on other sites More sharing options...
ignace Posted July 4, 2010 Share Posted July 4, 2010 $doc = new DomDocument(); $doc->formatOutput = true; $root = $doc->createElement('rss'); $root->setAttribute('version', '2.0'); $channel = $doc->createElement('channel'); while($row = $connection->fetch_assoc()) { .. $channel->addElement(..); } $root->addElement($channel); $doc->addElement($root); echo $doc->saveXML(); Quote Link to comment https://forums.phpfreaks.com/topic/206658-xml-createelement/#findComment-1080953 Share on other sites More sharing options...
V Posted July 4, 2010 Author Share Posted July 4, 2010 Thank you Ignace! I tried the code but I get Call to undefined method DOMElement::addElement() for $root->addElement($channel); $doc->addElement($root); I tried like this $doc = new DOMDocument(); $doc->formatOutput = true; $root = $doc->createElement('rss'); $root->setAttribute('version', '2.0'); $r = $doc->createElement( "channel" ); $doc->appendChild( $r ); while ($row = $result->fetch_assoc()) { //remember to use htmlentities $post_title = $row['post_title']; $post_content = $row['post_content']; $b = $doc->createElement( "item" ); $title = $doc->createElement( "title" ); $title->appendChild( $doc->createTextNode( $post_title ) ); $b->appendChild( $title ); $description = $doc->createElement( "description" ); $description->appendChild( $doc->createTextNode( $post_content ) ); $b->appendChild( $description ); $r->appendChild( $b ); } $root->addElement($channel); $doc->addElement($root); echo $doc->saveXML(); $doc->save("rss/" .$category. ".xml") ..not sure what I did wrong :-\ Quote Link to comment https://forums.phpfreaks.com/topic/206658-xml-createelement/#findComment-1081000 Share on other sites More sharing options...
ignace Posted July 4, 2010 Share Posted July 4, 2010 addElement() should be appendChild() Quote Link to comment https://forums.phpfreaks.com/topic/206658-xml-createelement/#findComment-1081069 Share on other sites More sharing options...
V Posted July 4, 2010 Author Share Posted July 4, 2010 Oooh! Ok thanks! Quote Link to comment https://forums.phpfreaks.com/topic/206658-xml-createelement/#findComment-1081072 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.