CrimpJiggler Posted October 21, 2013 Share Posted October 21, 2013 SOLVED I always have trouble creating new entries in an XML file. So lets say this is my XML file: <file_list> <file> <name>File name 1</name> <description>Blah blah</name> </file> </file_list> and I want to add a new file to it with PHP. Heres what I do: $xml = simplexml_load_file("file.xml"); $xml->addChild("file"); and this is where it gets tricky. If I do $xml->file->addChild("name"), it won't add the <name> tag to my newly created <file> tag because I didn't specify that its the last <file> tag in the XML file that needs editing. I know I can use XPath to select the last entry in the XML file, but there must be a simpler way to do this. SOLUTION: I feel like a bit of an idiot now, the solution popped into my head as I was writing this thread. I just needed to add the new file entry to a variable, so: $xml = simplexml_load_file($xml_file); $new_entry = $xml->addChild('applet'); $new_entry->addChild('name','blah'); $xml->asXML($xml_file); Link to comment https://forums.phpfreaks.com/topic/283151-creating-new-xml-entries/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.