Php 在更新现有文件时创建新的XML文件

Php 在更新现有文件时创建新的XML文件,php,xml,Php,Xml,我已经通读了关于这个主题的许多相关问题,但似乎找不到一个突出显示保存到两个单独的XML文件的问题 上传电子书时,我正在创建一个新的XML文件。对于本例,它将保存在“new_file.xml”中。同时,我需要将新信息(本例中为)添加到名为“full\u collection\u file.xml”的完整集合文件中,并更新该文件。因此,每次上传新电子书时,“full_collection_file.xml”都会添加一个新的 下面是简单的“new_file.xml”的当前结构: 这对于创建第一个XML

我已经通读了关于这个主题的许多相关问题,但似乎找不到一个突出显示保存到两个单独的XML文件的问题

上传电子书时,我正在创建一个新的XML文件。对于本例,它将保存在“new_file.xml”中。同时,我需要将新信息(本例中为
)添加到名为“full\u collection\u file.xml”的完整集合文件中,并更新该文件。因此,每次上传新电子书时,“full_collection_file.xml”都会添加一个新的

下面是简单的“new_file.xml”的当前结构:


这对于创建第一个XML文件(“new_file.XML”)很好,但无论我在同一个区域内尝试做什么,我似乎都无法正确加载和更新“full_collection_file.XML”。我是否应该尝试分别完成这两件事,而不是同时完成?看起来是多余的,但我希望您可以同时运行2个DomDocument文件

您只需要第二个
DomDocument
对象,然后导入新的
产品
节点,并将其附加到所需位置:

$doc = new DomDocument('1.0', 'UTF-8');
$doc->load('full_collection_file.xml');
$node = $doc->importNode($product, true);
$doc->documentElement->appendChild($node);
显然,如果您不希望新的
节点位于
full\u collection\u file.xml
的document元素,那么您需要相应地更改最后一行。

从TML更新:

$xml = new DomDocument('1.0', 'UTF-8');

$message = $xml->createElement('ONIXMessage');
$release = $xml->createAttribute('release');
$release->value = '3.0';
$message->appendChild($release);

$header = $xml->createElement('Header');
$FromCompany = $xml->createElement('FromCompany','My Company');
$header->appendChild($FromCompany);
$FromEmail = $xml->createElement('FromEmail','support@ebooks.com');
$header->appendChild($FromEmail);
$SentDate = $xml->createElement('SentDate','201408181805');
$header->appendChild($SentDate);
$message->appendChild($header);

$product = $xml->createElement('Product');
$RecordReference = $xml->createElement('RecordReference','B00BBE4BFE'); 
$product->appendChild($RecordReference);
$NotificationType = $xml->createElement('NotificationType','03');
$product->appendChild($NotificationType);  
$message->appendChild($product);

$xml->appendChild($message);
$xml->save('new_file.xml');

$full_xml = new DomDocument('1.0', 'UTF-8');
$full_xml->load('full_collection_file.xml');
$node = $full_xml->importNode($product, true);
$full_xml->documentElement->appendChild($node);
$full_xml->save('full_collection_file.xml');

重要的是我错过了什么;使用了6行代码来完成1的功能。谢谢,我会用完整的答案更新问题。@Jongware明白了。谢谢
$xml = new DomDocument('1.0', 'UTF-8');

$message = $xml->createElement('ONIXMessage');
$release = $xml->createAttribute('release');
$release->value = '3.0';
$message->appendChild($release);

$header = $xml->createElement('Header');
$FromCompany = $xml->createElement('FromCompany','My Company');
$header->appendChild($FromCompany);
$FromEmail = $xml->createElement('FromEmail','support@ebooks.com');
$header->appendChild($FromEmail);
$SentDate = $xml->createElement('SentDate','201408181805');
$header->appendChild($SentDate);
$message->appendChild($header);

$product = $xml->createElement('Product');
$RecordReference = $xml->createElement('RecordReference','B00BBE4BFE'); 
$product->appendChild($RecordReference);
$NotificationType = $xml->createElement('NotificationType','03');
$product->appendChild($NotificationType);  
$message->appendChild($product);

$xml->appendChild($message);
$xml->save('new_file.xml');

$full_xml = new DomDocument('1.0', 'UTF-8');
$full_xml->load('full_collection_file.xml');
$node = $full_xml->importNode($product, true);
$full_xml->documentElement->appendChild($node);
$full_xml->save('full_collection_file.xml');