使用php在节点之间附加xml数据

  $employees = array(); 
  $employees [] = array( 
  'name' => 'Albert', 
  'age' => '34', 
  'salary' => "$1000000000" 
  ); 
  $employees [] = array( 
  'name' => 'Claud', 
  'age' => '20', 
  'salary' => "$200000000" 
  ); 

  $doc = new DOMDocument(); 
  $doc->load('xml/text.xml');
  $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 ); 
  } 


  $doc->save("xml/text.xml") 

this existing code load and writes data to an xml file, however right now it keeps creating the parent node "employees" over and over again. How would I just append the child nodes to the already existing employees node in the xml file?

Assuming your root node isn't employees, and that there is just one employees node, replace these lines:

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

With these:

$tags = $doc->getElementsByTagName('employees');
if ($tags->length) {
    $r = $tags->item(0);
} else {
    $r = $doc->createElement('employees');
    $doc->appendChild( $r );
}

This code uses the first employees node found in the document. If none is found, it appends one to the end of the document. Actually, I'm guessing you want to insert the employees node somewhere inside the document, instead of at the end....

Assuming you already have an XML structure in xml/text.xml with a root node of 'employees' you want to replace this line:

$r = $doc->createElement( 'employees' );

with this line:

$r = $doc->documentElement;