Jump to content

[SOLVED] Need of Help XML PHP


vinchenzison

Recommended Posts

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

<?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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.