从post中检索信息并将其放入php中格式化的xml中

从post中检索信息并将其放入php中格式化的xml中,php,xml,post,Php,Xml,Post,我有一篇帖子,我需要检索一些数据并将其放入xml中 我的xml格式如下 <maintag> <item> postinfo (variable = value) <item> <maintag> postinfo(变量=值) 我只得到变量=值。我不知道如何为每个帖子创建一个项目标签,并将其放在maintag下 这是我的php代码 <?php header ('Location: http:

我有一篇帖子,我需要检索一些数据并将其放入xml中

我的xml格式如下

<maintag>

      <item>
          postinfo (variable = value)
      <item>

<maintag>

postinfo(变量=值)
我只得到变量=值。我不知道如何为每个帖子创建一个项目标签,并将其放在maintag下

这是我的php代码

<?php
header ('Location: http:thanskforpostingpage.html');
$handle = fopen("postedinfo.xml", "a");
foreach($_POST as $variable => $value) {
  fwrite($handle, $variable);
  fwrite($handle, "=");
  fwrite($handle, $value);
  fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fclose($handle);
exit;
?> 

发布后,xml应该如下所示

<maintag>



      <item>
          postinfo (variable = value)
      <item>

      <item>
          postinfo (variable = value)
      <item>

<maintag>

postinfo(变量=值)
postinfo(变量=值)
请注意,它已经添加了一个新的项目标签,但主标签仍然保持不变
. 因此,每篇文章都会在xml中添加一些新信息,您还需要实际输出xml标记

$handle = fopen(...);
fwrite($handle, "<maintag>");
foreach(...) {
    fwrite($handle, "<item>");
    fwrite($handle, $variable);
    ...
    fwrite($handle, "</item">);
}
fwrite($handle, "</maintag>");
fclose($handle);
$handle=fopen(…);
fwrite($handle,“”);
foreach(…){
fwrite($handle,“”);
fwrite($handle,$variable);
...

fwrite($handle,"谢谢你的回答。我需要在maintag中添加项目,这样我就不会创建新的maintag了?对不起,这个问题我在php中很笨。我应该告诉php搜索maintag,并按照你说的那样在它下面写。写下你有一个现有的xml文件,你需要添加更多的
标记来操作文件如果你是从头开始构建一个新的xml,那么我上面的代码将是一个方向。你的问题是:你在哪里遇到了障碍?