hi,
im trying to get a html/php form to either generate or add to an xml file depending on whether or not that file is already created.
For the file not existing, i have this to create the xml file:
[php]$xml = simplexml_load_file("./xml_files/".$user_id.".xml");
$item = new SimpleXMLElement("<items></items>");
$hi = $item->addChild("item");
$hi->addChild("itemName",$item_n);
$hi->addChild("itemSource",$source);
$item->asXML("./xml_files/".$user_id.".xml");
[/php]this generates the xml when it's executed.
for the case where the file already exists, assuming the first tag is <items> just like the created one in the last example, i have this:
[php]
$xml = simplexml_load_file("./xml_files/".$user_id.".xml");
$item = new SimpleXMLElement($xml->asXML());
$by = $item->addChild("item");
$by->addChild("itemName",$item_n);
$by->addChild("itemSource",$source);
$item->asXML("./xml_files/".$user_id.".xml");
[/php]Both of these work on their own(ie. I remove the xml and the second method for the 1st example, or leave an xml file in the directory and delete the first method).
The problem is when i try to combine them... as i said, the plan was to use either 1 depending on if the xml file already existed or not, so i thought i'd use the 'if (file_exists(file))' method. Here's what i have :
[php]
$filename = "./xml_files/".$user_id.".xml";
if (!file_exists($filename)) {
$xml = simplexml_load_file("./xml_files/".$user_id.".xml");
$item = new SimpleXMLElement("<items></items>");
$hi = $item->addChild("item");
$hi->addChild("itemName",$item_n);
$hi->addChild("itemSource",$source);
$item->asXML("./xml_files/".$user_id.".xml");
}else{
$xml = simplexml_load_file("./xml_files/".$user_id.".xml");
$item = new SimpleXMLElement($xml->asXML());
$by = $item->addChild("item");
$by->addChild("itemName",$item_n);
$by->addChild("itemSource",$source);
$item->asXML("./xml_files/".$user_id.".xml");
}
[/php]Can anyone see why this won't work or offer a suggestion please? ive been at it for hours

cheers