-2

I'm working on an assignment for school to create an online store. There is a page where the admin can update the item inventory and/or put items on sale. Every time an item is put on sale or taken off from sale, a function is called to update the RSS feed with the latest sale items.

Here is the RSS XML structure:

<?xml version="1.0"?>
<rss version="2.0">
    <channel>
        <title>Project2 RSS Feed</title>
        <language>en-us</language>
        <link>http://people.rit.edu/~cns3946/539/project2/project2.rss</link>
        <description>Project 2 RSS Feed</description>
        <item>
            <title>Far Cry 3</title>
            <link>http://people.rit.edu/cns3946/539/project2/index.php?item_num=3</link>
            <description>
            <![CDATA[a boring description goes here....]]></description>
            <price>59.99</price>
            <salePrice>49.99</salePrice>
            <pubDate>Sun, 03 Feb 2013 01:20:04 -0500</pubDate>
        </item>
        <item>
            <title>Tales of the Abyss 3DS</title>
            <link>http://people.rit.edu/cns3946/539/project2/index.php?item_num=13</link>
            <description><![CDATA[a boring description goes here....]]></description>
            <price>39.99</price>
            <salePrice>29.99</salePrice>
            <pubDate>Sun, 03 Feb 2013 01:20:04 -0500</pubDate>
        </item>
        <item>
            <title>Resistance: Fall of Man</title>
            <link>http://people.rit.edu/cns3946/539/project2/index.php?item_num=17</link>
            <description><![CDATA[a boring description goes here....]]></description>
            <price>19.99</price>
            <salePrice>9.99</salePrice>
            <pubDate>Sun, 03 Feb 2013 01:20:04 -0500</pubDate>
        </item>
    </channel>
</rss>

Every time the function is called, it appends to the file. I want to be able to remove all of the nodes and then append the new sale items to the file. I've tried using removeChild and couldn't figure it out. If you have any tips or pointers on how I can make this work, could you let me know? Any help would be appreciated.

  • If you want to remove *all* the nodes, then I think the best solution is just re-write the entire file. Getting the initial variables (title, language, etc.) shouldn't be a problem (especially if you make the program modular), and you can write everything else from scratch. – Ynhockey Feb 03 '13 at 09:16

3 Answers3

0

something like this should work:

$dom = new DOMDocument;
$dom->load('your_file_name.xml');
//loop thru each item node
foreach ($dom->getElementsByTagName('item') as $item) {
    //remove the item node
    $item->parentNode->removeChild($item);
}
echo $dom->saveXml();
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
0
$items = $domdocument->getElementsByTagName("item");
while($items->length > 0)
    $items->item(0)->parentNode->removeChild($items->item(0));
0

you Can Use preg_replace like This but it for string

$str=preg_replace("~<item(.*?)>(.*?)</item>~si",""," ".$str." ");

it is remove all item from string

mohammad mohsenipur
  • 3,218
  • 2
  • 17
  • 22