PHP 不使用 DOMDocument 保存

发布于 2024-11-27 19:43:14 字数 3407 浏览 2 评论 0原文

我们将直接从代码开始:

PHP:

<?php
    $newJaCourse = $_POST["ja-new-course"];
    $newJaSide = $_POST["ja-new-side"];
    $newEnCourse = $_POST["en-new-course"];
    $newEnSide = $_POST["en-new-side"];

    $doc = new DOMDocument('1.0','utf-8');
    $doc->formatOutput=true;
    $doc->preserveWhiteSpace=false;
    echo $doc->load("../../xml/daily_lunch.xml") . "Loaded <br />";

    $japanese = $doc->getElementsByTagName("japanese")->item(0);
    $jacourse = $japanese->getElementsByTagName("course")->item(0);
    $jaside = $japanese->getElementsByTagName("side")->item(0);
    $english = $doc->getElementsByTagName("english")->item(0);
    $encourse = $english->getElementsByTagName("course")->item(0);
    $enside = $english->getElementsByTagName("side")->item(0);

    $jacourse->nodeValue = $newJaCourse;
    $jaside->nodeValue = $newJaSide;
    $encourse->nodeValue = $newEnCourse;
    $enside->nodeValue = $newEnSide;

    $japanese->replaceChild($jacourse,$jacourse);
    $japanese->replaceChild($jaside,$jaside);
    $english->replaceChild($encourse,$encourse);
    $english->replaceChild($enside,$enside);

    echo $doc->save("../../xml/daily_lunch.xml") . "Done!";
?>

我精致的 HTML 表单:

        <h4>Change Today's Lunch Menu:</h4>
        <form method="post" action="scripts/lunchupdate.php" name="changelunch">
            <table>
                <tr>
                    <th>Japanese: Course</th>
                    <td><input type="textbox" name="ja-new-course" /></td>      
                </tr>
                <tr>
                    <th>Japanese: Side</th>
                    <td><input type="textbox" name="ja-new-side" /></td>        
                </tr>
                <tr>
                    <th>English: Course</th>
                    <td><input type="textbox" name="en-new-course" /></td>      
                </tr>
                <tr>
                    <th>English: Side</th>
                    <td><input type="textbox" name="en-new-side" /></td>        
                </tr>
            </table>
            <a href="javascript:" onclick="writeNewLunch()">Set Menu</a> |
            <a href="javascript:" onclick="document.changelunch.reset()">Reset Input</a>
        </form>

我当前的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<lunch featuredisabled="false">
    <japanese>
        <course>
            えびトマトクリームパスタ
        </course>
        <side>
            シーザーサラダ
        </side>
    </japanese>
    <english>
        <course>
            Shrimp and Tomato Cream Pasta
        </course>
        <side>
            Ceasar Side Salad
        </side>
    </english>
</lunch>

虽然您可能知道其目的,但它是更改 XML 文件中的项目。双语餐厅的每日午餐更新是我们的目标。我精彩的 DOMDocument->save(filename) 却不能做到这一点。

什么都没有返回,我所有的变量和变化都回响。我正在运行 PHP5。我查看了 phpinfo(),发现所有 XML 需求都在其中配置、打开,但没有完成它们的工作。

该脚本输出的唯一内容是:

已加载

完成!

We'll start off straight forward with the code:

The PHP:

<?php
    $newJaCourse = $_POST["ja-new-course"];
    $newJaSide = $_POST["ja-new-side"];
    $newEnCourse = $_POST["en-new-course"];
    $newEnSide = $_POST["en-new-side"];

    $doc = new DOMDocument('1.0','utf-8');
    $doc->formatOutput=true;
    $doc->preserveWhiteSpace=false;
    echo $doc->load("../../xml/daily_lunch.xml") . "Loaded <br />";

    $japanese = $doc->getElementsByTagName("japanese")->item(0);
    $jacourse = $japanese->getElementsByTagName("course")->item(0);
    $jaside = $japanese->getElementsByTagName("side")->item(0);
    $english = $doc->getElementsByTagName("english")->item(0);
    $encourse = $english->getElementsByTagName("course")->item(0);
    $enside = $english->getElementsByTagName("side")->item(0);

    $jacourse->nodeValue = $newJaCourse;
    $jaside->nodeValue = $newJaSide;
    $encourse->nodeValue = $newEnCourse;
    $enside->nodeValue = $newEnSide;

    $japanese->replaceChild($jacourse,$jacourse);
    $japanese->replaceChild($jaside,$jaside);
    $english->replaceChild($encourse,$encourse);
    $english->replaceChild($enside,$enside);

    echo $doc->save("../../xml/daily_lunch.xml") . "Done!";
?>

My exquisite HTML Form:

        <h4>Change Today's Lunch Menu:</h4>
        <form method="post" action="scripts/lunchupdate.php" name="changelunch">
            <table>
                <tr>
                    <th>Japanese: Course</th>
                    <td><input type="textbox" name="ja-new-course" /></td>      
                </tr>
                <tr>
                    <th>Japanese: Side</th>
                    <td><input type="textbox" name="ja-new-side" /></td>        
                </tr>
                <tr>
                    <th>English: Course</th>
                    <td><input type="textbox" name="en-new-course" /></td>      
                </tr>
                <tr>
                    <th>English: Side</th>
                    <td><input type="textbox" name="en-new-side" /></td>        
                </tr>
            </table>
            <a href="javascript:" onclick="writeNewLunch()">Set Menu</a> |
            <a href="javascript:" onclick="document.changelunch.reset()">Reset Input</a>
        </form>

My Current XML:

<?xml version="1.0" encoding="UTF-8"?>
<lunch featuredisabled="false">
    <japanese>
        <course>
            えびトマトクリームパスタ
        </course>
        <side>
            シーザーサラダ
        </side>
    </japanese>
    <english>
        <course>
            Shrimp and Tomato Cream Pasta
        </course>
        <side>
            Ceasar Side Salad
        </side>
    </english>
</lunch>

Although you can probably tell the purpose, it is to change the items in an XML file. A daily lunch updater for a bilingual restaurant is the goal. My wonderful DOMDocument->save(filename) doesn't do jack though.

Nothing is returned, all my variables and changes echo. I am running PHP5. I looked in phpinfo() and found where all the XML requirements are configured, turned on and not doing their job.

The only thing that comes output form this script is:

Loaded

Done!

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

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

发布评论

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

评论(2

守望孤独 2024-12-04 19:43:14

文件名丢失。根据PHP文档你应该有

$doc->save('myfile.xml');

你有权限写入文件夹?

Filename is missing. According to the PHP documentation you should have

$doc->save('myfile.xml');

Do you have permissions to write into folder?

你的背包 2024-12-04 19:43:14

我认为 $doc->save() 需要传递文件名。因此,将 $doc-save() 替换为 $doc->save("../../xml/daily_lunch.xml")

http://php.net/manual/en/domdocument.save.php

I think $doc->save() needs to have the file name passed. So replace $doc-save() with $doc->save("../../xml/daily_lunch.xml")

http://php.net/manual/en/domdocument.save.php

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