こんにちは、新しいファイルを作成して保存しない場合は、既存のファイルにデータを追加しようとしています。代わりに、私のコードはデータを 1 回だけ保存します。次のデータ セットは最初のデータ セットを上書きするだけです。私が間違っていることを理解するのを手伝ってください....
コメントの後、コードを変更しましたが、最終的には 警告: DOMDocument::load() [domdocument.load]: Extra content at the end of the document in になります
function toXml($reg)
{
$xmlFile = "customers.xml";
$doc = new DomDocument('1.0');
$doc->load( $xmlFile);
$cusCart = $doc -> createElement('cusCart');
//$cusCart = $doc->appendChild($cusCart);
foreach ($reg as $item => $val )
{
$customer = $doc->createElement('customer');
$customer = $cusCart->appendChild($customer);
$cusId = $doc->createElement('cusId');
$cusId = $customer->appendChild($cusId);
$value = $doc->createTextNode($item);
$value = $cusId->appendChild($value);
$fName = $doc->createElement('fName');
$fName = $customer->appendChild($fName);
$value1 = $doc->createTextNode($val["fName"]);
$value1 = $fName->appendChild($value1);
$lName = $doc->createElement('lName');
$lName = $customer->appendChild($lName);
$value2 = $doc->createTextNode($val["lName"]);
$value2 = $lName->appendChild($value2);
$phone = $doc->createElement('phone');
$phone = $customer->appendChild($phone);
$value3 = $doc->createTextNode($val["phone"]);
$value3 = $phone->appendChild($value3);
$email = $doc->createElement('email');
$email = $customer->appendChild($email);
$value4 = $doc->createTextNode($val["email"]);
$value4 = $email->appendChild($value4);
$pwd = $doc->createElement('pw');
$pwd = $customer->appendChild($pwd);
$value5 = $doc->createTextNode($val["pw"]);
$value5 = $pwd->appendChild($value5);
}
$cusCart = $doc->appendChild($cusCart);
$doc->save($xmlFile);
$strXml = $doc->saveXML();
echo("<br>----- DATA RECOREDED!!! ----");
return $strXml;
}