vinchenzison Posted July 18, 2008 Share Posted July 18, 2008 Hi, I'm looking for some help, been trying this for a couple of days and searched everywhere for help but I am fairly new to coding so getting stuck! I have an XML news database and want to be able to delete items from it. This is the xml document... <bignews> <item id="LKszKAWw"> <ndate>18.7.2008</ndate> <news>ff</news> <newsmain>ff</newsmain> <nurl>ff</nurl> <uniq>LKszKAWw</uniq> </item> <item id="JDMR8eqp"> <ndate>18.7.2008</ndate> <news>yes</news> <newsmain>yes</newsmain> <nurl>yes</nurl> <uniq>JDMR8eqp</uniq> </item> <item id="kh2y7BUK"> <ndate>18.7.2008</ndate> <news>no</news> <newsmain>no</newsmain> <nurl>no</nurl> <uniq>kh2y7BUK</uniq> </item> <item id="3UWXZAPP"> <ndate>18.7.2008</ndate> <news>maybe</news> <newsmain>mabye</newsmain> <nurl>maybe</nurl> <uniq>3UWXZAPP</uniq> </item> </bignews> So far I have this php code to try and delete an item but it's not working. <?php $dom=new DOMDocument; $dom->formatOutput = true; $dom->preserveWhiteSpace = false; $dom->validateOnParse = true; $dom->load('newsF8.xml'); $headlines=$dom->getElementsByTagName('item'); foreach($headlines as $headline){ if ($headline->getAttribute('id') == $_GET['num']) { while($headline->hasChildren()){ $headline->removeChild($headline->childNodes->item(0)); } } } $dom->save("newsF8.xml") or die ("Error saving to file"); ?> Any pointers?? Thanks guys (and gals) Link to comment https://forums.phpfreaks.com/topic/115415-solved-need-of-help-xml-php/ Share on other sites More sharing options...
unkwntech Posted July 18, 2008 Share Posted July 18, 2008 <?php // get contents of a file into a string $filename = "/usr/local/something.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); $find = "What you want to remove"; $new = preg_replace($find, '', $contents); fwrite($handle, $new); fclose($handle); ?> This is most likely incorrect, I'm not to good with the file handling, however the idea is there. Link to comment https://forums.phpfreaks.com/topic/115415-solved-need-of-help-xml-php/#findComment-593317 Share on other sites More sharing options...
samshel Posted July 18, 2008 Share Posted July 18, 2008 does it remove only the first sub element or it does nothing? Link to comment https://forums.phpfreaks.com/topic/115415-solved-need-of-help-xml-php/#findComment-593327 Share on other sites More sharing options...
vinchenzison Posted July 18, 2008 Author Share Posted July 18, 2008 At the moment it does nothing. I'm using a delete.php?num=**** to define what to look for. Using XML so not sure the code unkwntech would work for what I want Link to comment https://forums.phpfreaks.com/topic/115415-solved-need-of-help-xml-php/#findComment-593331 Share on other sites More sharing options...
samshel Posted July 18, 2008 Share Posted July 18, 2008 the code works for me using hasChildNodes() instead of using hasChildren()... also it deletes all the child nodes but not the main node with id specified... i use PHP5 Link to comment https://forums.phpfreaks.com/topic/115415-solved-need-of-help-xml-php/#findComment-593338 Share on other sites More sharing options...
vinchenzison Posted July 18, 2008 Author Share Posted July 18, 2008 Amazing, thank you so much! Been scratching my head for ages and then one simple change and it works! Just need to remove the empt node now :-) Thanks again Link to comment https://forums.phpfreaks.com/topic/115415-solved-need-of-help-xml-php/#findComment-593372 Share on other sites More sharing options...
samshel Posted July 18, 2008 Share Posted July 18, 2008 please mark topic as solved. Link to comment https://forums.phpfreaks.com/topic/115415-solved-need-of-help-xml-php/#findComment-593375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.