$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")
此現有代碼加載並將數據寫入到xml文件,但是現在它一直重複創建父節點「employees」。我如何將子節點追加到xml文件中已存在的員工節點?在使用php的節點之間添加xml數據
原本文檔中是否只有一個'員工'節點? – 2011-03-05 04:59:57