Jump to content

Creating new XML entries


CrimpJiggler

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.