CrimpJiggler Posted October 21, 2013 Share Posted October 21, 2013 (edited) 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); Edited October 21, 2013 by CrimpJiggler Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.