让 PHP 覆盖 XML 文件?

发布于 2024-11-19 11:32:29 字数 91 浏览 0 评论 0原文

我希望当有人在表单上点击“提交”时,打开一个 xml 文件并用表单生成的新数据覆盖它的所有内容。

PHP 可以做到吗?我应该从什么开始?有什么例子吗?

I want when someone hits 'submit' on a form to then open an xml file and overwrite all of it's contents with new data generate from the form.

Can PHP do that? What should I look for to start? Any examples?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁梦 2024-11-26 11:32:29

听起来您才刚刚开始。
您需要研究以下内容:

  1. POST 请求
  2. PHP 中的文件操作

另外,如果您有 php 特定问题,请将其简化为一个单词并将其添加到 php.net/ 的末尾

例如,尝试访问 php.net/xml(您将找到有关使用 XML 的文档)

另外,无论谁修改了原始海报 - 来吧,伙计。如果您认为这个问题太基础了,请继续。我们都必须从某个地方开始。

Sounds like you are just getting started.
You need to research the following:

  1. POST requests
  2. File operations in PHP

Also, if you have a php specific question, distill it down to a word and add it at the end of php.net/

For example, try going to php.net/xml (you will find documentation on working with XML)

Also, whoever modded the original poster down - cmon, man. If you think it's too base of a question just move on. We all have to start somewhere.

梦断已成空 2024-11-26 11:32:29

1 - 填写表格以填写数据
2 - 将表单数据发送到操作页面(如果您愿意,可以是同一页面)
3 - 将表单中的数据写入 XML 文件。
4 - 完成了!

我不明白为什么你需要交换数据。
非常简单,您只需使用所需的数据创建一个新的 XML 文件即可。
看看下面这个示例。

    // YOUR DATA FROM METHOD POST OR GET
       $doc1 = $_POST['doc1'];
       ... and so on 


    // Start XML file, echo parent node
       $data = '<sistema versao="1.02" encoding="utf-8">';

    // ADD TO XML DOCUMENT NODE


  $data .=  '<dest>';
      $data .=  '<doc>' . parseToXML($doc1) . '</doc>';
      $data .=  '<xName>' . parseToXML($name) . '</xName>';
      $data .=  '<addrDest>';
        $data .=  '<xLgr>' . parseToXML($addr) . '</xLgr>';
        $data .=  '<nro>' . parseToXML($num) . '</nro>';
        $data .=  '<xCpl>' . parseToXML($comp) . '</xCpl>';
        $data .=  '<xSub>' . parseToXML($suburb) . '</xSub>';
        $data .=  '<cMun>3523206</cMun>';
        $data .=  '<xMun>' . parseToXML($city) . '</xMun>';
        $data .=  '<UF>' . parseToXML($state) . '</UF>';
        $data .=  '<CEP>' . parseToXML($zip) . '</CEP>';
        $data .=  '<cCoun>1058</cCoun>';
        $data .=  '<xCoun>BRASIL</xCoun>';
        $data .=  '<phone>' . parseToXML($tel1) . '</phone>';
      $data .=  '</addrDest>';
      $data .=  '<email>' . parseToXML($email) . '</email>';
      $data .=  '</dest>';


        // End XML file
        $data .=  '</sistema>';


        # Put data into client.xml 
        file_put_contents('client.xml', $data);

我希望这能给你一个开始。祝你好运!

1 - Have the form to fill the datas
2 - Send the data of the form to an action page (it can be the same page if you wish)
3 - Write to an XML file the datas from the form.
4 - It is done!

I do not understood why you need to exchange the data.
Is much easy you just create a new XML file with the data you wish.
Take a look at this sample below.

    // YOUR DATA FROM METHOD POST OR GET
       $doc1 = $_POST['doc1'];
       ... and so on 


    // Start XML file, echo parent node
       $data = '<sistema versao="1.02" encoding="utf-8">';

    // ADD TO XML DOCUMENT NODE


  $data .=  '<dest>';
      $data .=  '<doc>' . parseToXML($doc1) . '</doc>';
      $data .=  '<xName>' . parseToXML($name) . '</xName>';
      $data .=  '<addrDest>';
        $data .=  '<xLgr>' . parseToXML($addr) . '</xLgr>';
        $data .=  '<nro>' . parseToXML($num) . '</nro>';
        $data .=  '<xCpl>' . parseToXML($comp) . '</xCpl>';
        $data .=  '<xSub>' . parseToXML($suburb) . '</xSub>';
        $data .=  '<cMun>3523206</cMun>';
        $data .=  '<xMun>' . parseToXML($city) . '</xMun>';
        $data .=  '<UF>' . parseToXML($state) . '</UF>';
        $data .=  '<CEP>' . parseToXML($zip) . '</CEP>';
        $data .=  '<cCoun>1058</cCoun>';
        $data .=  '<xCoun>BRASIL</xCoun>';
        $data .=  '<phone>' . parseToXML($tel1) . '</phone>';
      $data .=  '</addrDest>';
      $data .=  '<email>' . parseToXML($email) . '</email>';
      $data .=  '</dest>';


        // End XML file
        $data .=  '</sistema>';


        # Put data into client.xml 
        file_put_contents('client.xml', $data);

I hope this give a start to you. Good Luck!

信仰 2024-11-26 11:32:29

是的,使用

  • fwrite

    $toWrite; // 从表单生成的数据,格式化为 XML
    $fp = fopen('data.xml', 'w');
    fwrite($fp, $toWrite);
    fclose($fp);
    

Yes, using

  • fwrite

    $toWrite; // data generated from form, formatted to XML
    $fp = fopen('data.xml', 'w');
    fwrite($fp, $toWrite);
    fclose($fp);
    
撩动你心 2024-11-26 11:32:29

这看起来已经得到了回答,但是 fwrite() 决议并没有真正解释它如何为我们新手工作。我使用 file_put_contents 代替,但这是一个工作示例...

$writeXML = new XMLWriter();
$writeXML->openMemory();
$writeXML->setIndent(true);
$writeXML->startDocument("1.0","UTF-8");
$writeXML->startElement($root);

//foreach loop iterating through array results and writing elements

$writeXML->endDocument();
//this will overwrite the xml file
file_put_contents('xmlfile.xml', $writeXML->flush(true));
//this will append any data to the end of the file
file_put_contents('xmlfile.xml', $writeXML->flush(true), FILE_APPEND);

希望这有帮助... file_put_contents: PHP 手册

This looks answered already, but the fwrite() resolution doesn't really explain how it works for us novices. I used file_put_contents instead, but here is a working example...

$writeXML = new XMLWriter();
$writeXML->openMemory();
$writeXML->setIndent(true);
$writeXML->startDocument("1.0","UTF-8");
$writeXML->startElement($root);

//foreach loop iterating through array results and writing elements

$writeXML->endDocument();
//this will overwrite the xml file
file_put_contents('xmlfile.xml', $writeXML->flush(true));
//this will append any data to the end of the file
file_put_contents('xmlfile.xml', $writeXML->flush(true), FILE_APPEND);

Hope this helps... file_put_contents: PHP Manual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文