0

HI Guys, Im kinda new to php and xml so pls bear with me.

I wanna how am I gonna append an xml data to an xml file without overwriting the existing data uisng PHP.

I have here the codes:

writexml.php

<?php 
 $employees = array(); 

 $employees [] = array( 
  'name' => 'Tom', 
  'age' => '34', 
  'salary' => "$10000" 
 ); 
 $employees [] = array( 
  'name' => 'Ryan', 
  'age' => '20', 
  'salary' => "$2000" 
 ); 
 $employees [] = array( 
  'name' => 'Dave', 
  'age' => '20', 
  'salary' => "$2000" 
 ); 

 $doc = new DOMDocument(); 
 $doc->formatOutput = true; 

 $r = $doc->createElement( "employees" ); 
 $doc->appendChild( $r ); 

 foreach( $employees as $employee ) 
 { 
  $b = $doc->createElement( "employee" ); 

  $name = $doc->createElement( "name" ); 
  $name->appendChild( 
   $doc->createTextNode( $employee['name'] ) 
  ); 
  $b->appendChild( $name ); 

  $age = $doc->createElement( "age" ); 
  $age->appendChild( 
   $doc->createTextNode( $employee['age'] ) 
  ); 
  $b->appendChild( $age ); 

  $salary = $doc->createElement( "salary" ); 
  $salary->appendChild( 
   $doc->createTextNode( $employee['salary'] ) 
  ); 
  $b->appendChild( $salary ); 

  $r->appendChild( $b ); 
 } 

 echo $doc->saveXML(); 
 $doc->save("employees.xml") 
?>

What happens when I run this code it removes all previous data. Pls help.

1 Answer 1

4

You will need to open the existing document to append information to it, your last save will simply overwrite the existing file.

$doc = new DomDocument();
$doc->loadXML(file_get_contents('employees.xml'));
foreach($doc->getElementsByTagName('employees') as $node)
{
  // your current xml logic here
}

Update for hafedh

$doc = new DomDocument();
$doc->formatOutput = true;
if($xml = file_get_contents('employees.xml'))
  $doc->loadXML($xml);
$nodelist = $doc->getElementsByTagName('employees');
if($nodelist->length === 0)
{
  $nodelist = $doc->createElement("employees"); 
  $doc->appendChild($nodelist);
  $nodelist = $doc->getElementsByTagName('employees');
}
foreach($nodelist as $key => $node)
{
  // Employee Container
  $element = $doc->createElement("employee");
  $employee = $node->appendChild($element);
  // Name Element
  $element = $doc->createElement("name");
  $name = $employee->appendChild($element);
  $element = $doc->createTextNode('CCC');
  $name->appendChild($element);
  // Age Element
  $element = $doc->createElement("age");
  $age = $employee->appendChild($element);
  $element = $doc->createTextNode('333');
  $age->appendChild($element);
}

echo '<pre>' . htmlentities($doc->saveXML());

xml file contents

<employees><employee><name>AAA</name><age>111</age></employee><employee><name>BBB</name><age>222</age></employee></employees>
Sign up to request clarification or add additional context in comments.

4 Comments

You mean I will get the existing data and save it again along with the new data?
Yes that is correct, you will also need to put logic into your code incase <employees> doesn't exist or to handle starting with a new XML document, but the above is a small example of locating an existing element in preperation for appending new child elements.
it may not be clear but you'll need to append the new <employee> element to $node (which will be the original <employees> element in the existing document)
Ultimately the code I added is used to locate an existing element, where $node[0] will be the first <employees> element it finds. from there you can add child elements off $node, if it doesn't find anything, then you know you need to create a new <employees> element within the document.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.