Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5967173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:54:21+00:00 2026-05-22T19:54:21+00:00

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

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:54:21+00:00Added an answer on May 22, 2026 at 7:54 pm

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hay guys. I'm kinda new to OOP in PHP. I've learnt how to write
I'm kinda new here and I've got a programming/coding related question you guys maybe
It may seem to be very easy for you guys but I'm kinda new
Hey guys IM Looking at Implementing MVVM in Silverlight. Kind of new to Silverlight
I'm kinda new to WPF and .Net programming in general. I have downloaded this
Hey guys, sorry I'm kind of new to programming, but I had a small
I'm kind of new in PHP. For some reason in other types of programming
hey guys, i'm completely new to cURL - i've never used it before. If
I'm kinda new to Java EE 6 and most of what I'm reading is
I'm new to Qt. This is simply great. I was wondering whether you guys

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.