使用PHP追加XML文件

使用PHP追加XML文件,php,xml,append,Php,Xml,Append,我有一个包含HTML表单的PHP文件。当用户在表单中输入数据时,它将执行错误检查,如果一切正常,则输入的数据将按特定顺序写入XML文件。这部分工作得很好。我的问题是,当用户再次填写此表单时,我需要将新数据附加到XML文件(而不是覆盖它)。目前,它只是过度写入数据,有人能帮助我如何做到这一点吗?我试着看教程,但我对XML非常陌生,发现它们令人困惑。我的XML文件如下所示 <?php function createFile($xml_file) { $FileMessageID =

我有一个包含HTML表单的PHP文件。当用户在表单中输入数据时,它将执行错误检查,如果一切正常,则输入的数据将按特定顺序写入XML文件。这部分工作得很好。我的问题是,当用户再次填写此表单时,我需要将新数据附加到XML文件(而不是覆盖它)。目前,它只是过度写入数据,有人能帮助我如何做到这一点吗?我试着看教程,但我对XML非常陌生,发现它们令人困惑。我的XML文件如下所示

<?php

function createFile($xml_file)
{

    $FileMessageID = $_POST['messageid'];
    $FileCreation = $_POST['filedatetime'];
    $FileTransactions = $_POST['filetransactions'];
    $FileControlSum = $_POST['filecontrolsum'];
    $CID = $_POST['CID'];

    // create new dom document
    $xml = new DOMDocument();

    // these lines would create a nicely indented XML file
    $xml->preserveWhiteSpace = false;
    $xml->formatOutput = true;

    // create a root element, and add it to DOM
    addRoot($xml);

    // add more elements to xml file
    $CustomerDDInfo = $xml->createElement("CstmrDrctDbtInitn");

    // add this element to the root
    $xml->documentElement->appendChild($CustomerDDInfo);

    // create elements
    $GroupHeader = $xml->createElement("GrpHdr");
    $InitiatingParty = $xml->createElement("InitgPty");
    $IdentificationHeading = $xml->createElement("Id");
    $PrivateIdentification = $xml->createElement("PrvtId");
    $Other = $xml->createElement("Othr");


    // append these elements to friend
    $CustomerDDInfo->appendChild($GroupHeader);
    $GroupHeader->appendChild($xml->createElement('MsgID', $_POST['messageid']));
    $GroupHeader->appendChild($xml->createElement('CreDtTm', $_POST['filedatetime']));
    $GroupHeader->appendChild($xml->createElement('NbOfTxs', $_POST['filetransactions']));
    $GroupHeader->appendChild($xml->createElement('CtrlSum', $_POST['filecontrolsum']));
    $GroupHeader->appendChild($InitiatingParty);
    $InitiatingParty->appendChild($IdentificationHeading);
    $IdentificationHeading->appendChild($PrivateIdentification);
    $PrivateIdentification->appendChild($Other);
    $Other->appendChild($xml->createElement('Id', $_POST['CID']));
    $CustomerDDInfo->appendChild($PaymentInformation);


    // save dom document to an xml file
    $xml->save($xml_file);


}

function addRoot(&$xml)
{
    $xml->appendChild($xml->createElement("entry"));
}

// call xml function
createFile('SEPA.xml');

//Redirect to Thank You Page
header ('Location: thankyou.php');

?>

  • 加载现有XML文档(如果有)
  • 在正确的级别添加一个子项,其中包含要附加的信息
XML不像文本文件,您只需将其他数据放在它的底部。在XML中,您必须将额外的信息放在现有XML中的某个节点中

<exampleXML>
   <item>
      <name></name>
      <number></number>
      <date></date>
   </item>
</exampleXML>

当将另一项添加到XML时,您应该加载XML,使用“exampleXML”节点并向其附加一个子项。 结果:


我不经常在PHP中使用XML DOM,因此我无法真正为您提供任何代码。
查看正确的函数。

谢谢,我会查看链接,希望我能更好地理解,因为现在我很困惑。不过谢谢你的回复!因此,我现有的XML文档名为SEPA.XML,代码是否类似;/创建新的dom文档$xml=newDOMDocument();//加载现有文件$xml->Load(“SEPA.xml”,LIBXML\u NOBLANKS);对的然后将您想要添加到XML中的新项设置为我的示例中的“item”节点。之后,您需要:$group=$xml->getElementsByTagName('group_NAME')$组->追加子项($NEW_项);这将从我的示例中获取“exmapleXML”节点,并将第二项添加到其中。
<exampleXML>
   <item>
      <name></name>
      <number></number>
      <date></date>
   </item>
   <item>
      <name></name>
      <number></number>
      <date></date>
   </item>
</exampleXML>