使用 PHP DOM 在 XML 文件中插入数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/194574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting data in XML file with PHP DOM
提问by liewl
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.
用户将访问 index.php,他将在其中看到 XML 文件中当前活动的列表,以及下方的文本字段,他可以在其中插入新活动。单击发送按钮后,页面将调用 insert.php,其中包含在 DOM 树中打开 XML 文件的代码,在根节点下插入一个新节点并回调用户应该在的 index.php 页面能够看到活动列表,他的新活动在其他人的下方。它不工作。当我单击按钮提交新条目时,页面刷新,显然没有任何反应,XML 与以前相同。我做错了什么?另外,我想知道是否有更好的方法。
回答by Owen
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.
注意:您缺少action="insert.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';
回答by Amdad Hossain
Final Solution
最终解决方案
sample.XML
样本.XML
<list>
<activity>swimming</activity>
<activity>running</activity>
<activity>Jogging</activity>
<activity>Theatre</activity>
<activity>Programming</activity>
</list>
index.php
索引.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
<?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');
?>
回答by Amdad Hossain
$newText = $xmldoc->createTextNode($newActv);
$newText = $xmldoc->createTextNode($newActv);
Change this line to
将此行更改为
$newText = $xmldoc->createTextNode($newAct);
$newText = $xmldoc->createTextNode($newAct);
回答by Amdad Hossain
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
现在尝试,它应该可以工作,这会有意义
回答by Amdad Hossain
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" ?>正确的代码是:
<?xml:stylesheet type="text/xsl" href="sample.xsl" ?>
回答by wicho
this is the code i work for me.
这是我为我工作的代码。
index.php
索引.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
示例.xml
<list>
<activity>swimming</activity>
<activity>running</activity>
</list>

