I want to append an existing XML file with a new entry and if possible I dont want to open/parse/re-write the whole file. Does anyone have any idea how?
I’ve tried using fgets() in a loop and evaluating the string it returns me to determine when the last XML tag is read but Im having trouble setting the file pointer to continue writing from just before that tag.
Take a look:
function save_changes(){
global $image_path;
$new_event = new xml_event();
$new_event->title = $_POST['new_gig_title'];
$new_event->blurb = $_POST['new_gig_info'];
$new_event->tickets = $_POST['new_ticket_info'];
$new_event->image = $image_path;
$file=fopen("xml_new.xml","a+");
while(!feof($file)){
echo fgets($file);
if(trim(fgets($file)) == "</gig>"){
echo "FOUND";
break;
}
}
fwrite($file, " <event>
");
$title = " <title>". $new_event->title ."</title>
";
fwrite($file, $title);
$blurb = " <blurb>". $new_event->blurb ."</blurb>
";
fwrite($file, $blurb);
$ticket = " <ticket>". $new_event->tickets ."</ticket>
";
fwrite($file, $ticket);
$image = " <image>". $new_event->image ."</image>
";
fwrite($file, $image);
fwrite($file, " </event>
");
fwrite($file, "</gig>");
fclose($file);
}
Its pretty strightforward - When fgets reads the line </gig> I want to break out of the loop and continue writing from just before that point… I think I should be using fseek() or rewind() but Im not sure how far back to set the pointer?? If I’ve got the right idea at all.
If anyone can help, I’d appreciate it.
Cheers.