Ick thats just nasty, it assumes that the XML file has a particular physical structure. If your convinced about using that approach, then you could just do
$f = file("file.xml");
$end = array_pop($f);
$f[count($f)] = "<new_element>foobar</new_element>";
$f[count($f)] = $end;
$fh = fopen("file.xml", "w+");
// Write file back etc
I Don't recommend it though :p