Jump to content

Writing to XML file help


alphawolf21

Recommended Posts

<?php
$doc = new DOMDocument();
$doc->load("rss/updates.xml");
$doc->formatOutput = true;
include 'functions.php';
connect();
query("INSERT INTO updates(post,userid,subject,posted)VALUES('".addslashes($_POST["post"])."','".$_COOKIE["id"]."','".addslashes($_POST["subject"])."',CURRENT_TIMESTAMP)");
$n = mysql_insert_id();
loguser($_COOKIE["id"],"added an update to the site.");
$update = sql("SELECT subject,post FROM updates WHERE id = '".$n."'");
$rss = 	$doc->createElement("rss");
	$version = $doc->createAttribute("version");
	$version->value = "2.0";
	$nameSpace = $doc->createAttribute("xml:ns");
	$nameSpace->value = "http://www.w3.org/2005/Atom";
$rss->appendChild($version);
$rss->appendChild($nameSpace);
$channel = $doc->createElement("channel");
$item = $doc->createElement("item");
$title = $doc->createElement("title");
	$title->appendChild(
		$doc->createTextNode(stripslashes($update["subject"]))
		);
$link = $doc->createElement("link");
$link->appendChild(
	$doc->createTextNode("http://www.dreamspand.com/?act=updates")
);
 $post = $doc->createElement("description");
 	$post->appendChild(
   		$doc->createTextNode(substr(stripslashes($update["post"]),0,80)."..")
   	);
$item->appendChild($link);
$item->appendChild($title);
$item->appendChild($post);
$channel->appendChild($item);
$rss->appendChild($channel);
$doc->appendChild($rss);
$doc->saveXML();
$doc->save("rss/updates.xml");
echo "location.href='?act=updates';";
?>

This function writes and saves to an XML file, with one problem. It creates the RSS and channel elements each time I post an update into my database. How do I get it to just append the update elements to these tags without creating more? As in, how do I write within the rss/channel tags alone, and not just keep adding more of them to the file?

 

My XML file looks like this:

<?xml version="1.0"?>
<rss version="2.0" xml:ns="http://www.w3.org/2005/Atom">
  <channel>
    <item>
      <link>http://www.dreamspand.com/?act=updates</link>
      <title>t5</title>
      <description>f5..</description>
    </item>
  </channel>
</rss>
<rss version="2.0" xml:ns="http://www.w3.org/2005/Atom">
  <channel>
    <item>
      <link>http://www.dreamspand.com/?act=updates</link>
      <title>t6</title>
      <description>f6..</description>
    </item>
  </channel>
</rss>

I want it to look like this:

<?xml version="1.0"?>
<rss version="2.0" xml:ns="http://www.w3.org/2005/Atom">
  <channel>
    <item>
      <link>http://www.dreamspand.com/?act=updates</link>
      <title>t5</title>
      <description>f5..</description>
    </item>
    <item>
      <link>http://www.dreamspand.com/?act=updates</link>
      <title>t6</title>
      <description>f6..</description>
    </item>
  </channel>
</rss>

So if anyone has any ideas, I'd really appreciate it please. Otherwise this works fine, I'm just kinda lost.

Link to comment
Share on other sites

$rss = $doc->createElement("rss");
$version = $doc->createAttribute("version");
$version->value = "2.0";
$nameSpace = $doc->createAttribute("xml:ns");
$nameSpace->value = "http://www.w3.org/2005/Atom";

 

Above the values are assigned, then added to the XML file below:

 

$doc->appendChild($rss);

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.