用PHP DOM插入XML文件中的数据 [英] Inserting data in XML file with PHP DOM

查看:89
本文介绍了用PHP DOM插入XML文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将新数据插入到现有的XML文件中,但是它不起作用。这是我的xml文件:

I was trying to insert new data into an existing XML file, but it's not working. Here's my xml file:

<list>
    <activity>swimming</activity>
    <activity>running</activity>
<list>

现在,我的想法是制作两个文件:索引页,显示文件中的内容,提供了一个用于插入新元素的字段,以及一个将数据插入到XML文件中的php页面。以下是index.php的代码:

Now, my idea was making two files: an index page, where it displays what's on the file and provides a field for inserting new elements, and a php page which will insert the data into the XML file. Here's the code for index.php:

<html>
<head><title>test</title></head>
</head>

<?php
    $xmldoc = new DOMDocument();
    $xmldoc->load('sample.xml', LIBXML_NOBLANKS);

    $activities = = $xmldoc->firstChild->firstChild;
    if($activities!=null){
        while(activities!=null){
            echo $activities->textContent.'<br/>';
            activities = activities->nextSibling.
        }
    }
?>

<form name='input' action='insert.php' method='post'>
    insert activity:
    <input type='text' name='activity'/>
    <input type='submit' value='send'/>
</form>
</body>
</html

这里是insert.php的代码:

and here's the code for insert.php:

<?php
    header('Location:index.php');
    $xmldoc = new DOMDocument();
    $xmldoc->load('sample.xml');

    $newAct = $_POST['activity'];

    $root = $xmldoc->firstChild;

    $newElement = $xmldoc->createElement('activity');
    $root->appendChild($newElement);
    $newText = $xmldoc->createTextNode($newAct);
    $newElement->appendChild($newText);

    $xmldoc->save('sample.xml');
?>

用户要访问index.php,在那里他将看到当前活动的列表XML文件和下面的文本字段,他可以在其中插入新的活动。单击发送按钮后,页面将调用insert.php,其中包含在DOM树中打开XML文件的代码,在根节点下插入一个新节点并调用index.php页面,用户应该在其中能够看到活动列表,他的新活动在其他的。它不工作当我点击按钮提交一个新条目时,页面将会刷新,显然没有任何反应,XML与以前一样。我做错了什么?另外,我想知道是否有更好的方法。

The user is to access index.php, where he would see a list of the current activities present in the XML file, and a text field below where he can insert new activities. Upon clicking the send button, the page would call insert.php, which contains a code that opens the XML file in a DOM tree, inserts a new node under the root node and calls back the index.php page, where the user should be able to see the list of activities, his new activity there under the others. It is not working. When i click on the button to submit a new entry, the pages refreshes and apparently nothing happens, the XML is the same as before. What did i do wrong? Also, i'd like to know if there's a better way of doing it.

推荐答案

是你的代码块复制和粘贴你现有的文件?如果是这样,我会看到两个潜在的问题:

is your code block copy and pasted from your existing files? if so i see two potential issues:

<form name='input' action'insert.php' method='post'> // should be:
<form name="input" action="insert.php" method="post">

注意:您缺少操作 strong> = insert.php,这将导致表单刚刚重新加载,而不提交,这是您描述的行为。

note: you're missing action="insert.php", which would cause the form to just reload itself without submitting, which is the behaviour you describe.

其次,请确保您具有sample.xml的写入权限。你可以确认你是否真的在写东西:

secondly, make sure you have write permission to "sample.xml". you can confirm if you're actually writing anything:

print 'I wrote '.$xmldoc->save('sample.xml').' bytes of data';

这篇关于用PHP DOM插入XML文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆