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);
Edited by CrimpJiggler
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.