Jump to content

XML createElement


V

Recommended Posts

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>

Link to comment
Share on other sites

$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();

Link to comment
Share on other sites

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  :-\

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.