【问题标题】:Appending an XML File Using PHP使用 PHP 附加 XML 文件
【发布时间】:2014-08-17 05:12:24
【问题描述】:

我有一个包含 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');

?>

【问题讨论】:

    标签: php xml append


    【解决方案1】:
    • 加载现有的 XML 文档(如果有的话)
    • 使用您要附加的信息在正确级别添加一个孩子

    XML 不像文本文件,您只需将其他数据放在它的底部。在 XML 中,您必须将额外信息放在现有 XML 中某处的节点中。

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

    当您应该加载 XML 的另一个项目时,获取“exampleXML”节点并向其附加一个子节点。 结果:

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

    我不经常在 PHP 中使用 XML DOM,所以我无法为您提供任何代码。 查看http://www.php.net/manual/en/class.domdocument.php 了解正确的功能。

    【讨论】:

    • 谢谢你,我会看看链接,希望我能更好地理解,因为现在我很困惑..谢谢你的回复!
    • 所以我现有的 XML 文档称为 SEPA.xml,代码会是这样的吗? // 创建新的 dom 文档 $xml = new DOMDocument(); //加载已有文件 $xml->load("SEPA.xml", LIBXML_NOBLANKS);
    • 正确。然后制作您想要添加到 XML 中的新项目,就像我的示例中的“项目”节点一样。之后,你会想要: $group = $xml->getElementsByTagName('GROUP_NAME'); $group->appendChild($NEW_ITEM);这将从我的示例中获取“exmapleXML”节点,并将第二项添加到其中。
    猜你喜欢
    • 2021-07-25
    • 2010-11-11
    • 2014-01-22
    • 2013-05-13
    • 2013-04-30
    • 1970-01-01
    • 2012-07-25
    相关资源
    最近更新 更多