使用PHP DOM在XML文件中插入数据

时间:2022-02-08 03:45:46

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

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

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

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:

现在,我的想法是创建两个文件:一个索引页,其中显示文件上的内容并提供一个用于插入新元素的字段;一个php页将数据插入XML文件。下面是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

and here's the code for insert.php:

这是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');
?>

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.

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

6 个解决方案

#1


8  

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">

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

注意:你失踪的行动= "插入。php“,这将导致表单在不提交的情况下重新加载自己,这就是您所描述的行为。

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

其次,确保您已经对“sample.xml”有了写权限。你可以确认你是否真的在写什么:

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

#2


3  

Final Solution

最终的解决方案

sample.XML

sample.XML

<list>
    <activity>swimming</activity>
    <activity>running</activity>
    <activity>Jogging</activity>
    <activity>Theatre</activity>
    <activity>Programming</activity>
</list>

index.php

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

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');
?>

#3


2  

$newText = $xmldoc->createTextNode($newActv);

$ newText xmldoc = $ - > createTextNode($ newActv);

Change this line to

改变这条线

$newText = $xmldoc->createTextNode($newAct);

$ newText xmldoc = $ - > createTextNode($ newAct);

#4


2  

Actually you made mistakes in two places.

实际上你在两个地方犯了错误。

This line should be I think because of the typo, you missed an equal sign. Also

这条线应该是我认为因为错字,你错过了一个等号。也

These lines should be

这些线应该是

Try now, it should work, Hop this would make some sense

现在试一下,应该能行,但愿这能说得通

#5


1  

this is the code i work for me.

这是我为我工作的代码。

index.php

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


<?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');
?>

sample.xml

sample.xml

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

#6


1  

I think I know what is the problem with your code. You should not write like that: <?xml-stylesheet type="text/xsl" href="sample.xsl" ?> The right code is:

我想我知道你的代码有什么问题。你不应该这样写:

<?xml:stylesheet type="text/xsl" href="sample.xsl" ?>

#1


8  

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">

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

注意:你失踪的行动= "插入。php“,这将导致表单在不提交的情况下重新加载自己,这就是您所描述的行为。

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

其次,确保您已经对“sample.xml”有了写权限。你可以确认你是否真的在写什么:

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

#2


3  

Final Solution

最终的解决方案

sample.XML

sample.XML

<list>
    <activity>swimming</activity>
    <activity>running</activity>
    <activity>Jogging</activity>
    <activity>Theatre</activity>
    <activity>Programming</activity>
</list>

index.php

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

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');
?>

#3


2  

$newText = $xmldoc->createTextNode($newActv);

$ newText xmldoc = $ - > createTextNode($ newActv);

Change this line to

改变这条线

$newText = $xmldoc->createTextNode($newAct);

$ newText xmldoc = $ - > createTextNode($ newAct);

#4


2  

Actually you made mistakes in two places.

实际上你在两个地方犯了错误。

This line should be I think because of the typo, you missed an equal sign. Also

这条线应该是我认为因为错字,你错过了一个等号。也

These lines should be

这些线应该是

Try now, it should work, Hop this would make some sense

现在试一下,应该能行,但愿这能说得通

#5


1  

this is the code i work for me.

这是我为我工作的代码。

index.php

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


<?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');
?>

sample.xml

sample.xml

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

#6


1  

I think I know what is the problem with your code. You should not write like that: <?xml-stylesheet type="text/xsl" href="sample.xsl" ?> The right code is:

我想我知道你的代码有什么问题。你不应该这样写:

<?xml:stylesheet type="text/xsl" href="sample.xsl" ?>