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.
You will need to open the existing document to append information to it, your last save will simply overwrite the existing file.
Update for hafedh
xml file contents