使用PHP添加XML节点

使用PHP添加XML节点,php,xml,nodes,Php,Xml,Nodes,我的XML开始时是这样的: <user> <entry> <date>December 8, 2012, 6:27 am</date> <height>73</height> <weight>201</weight> </entry> </user> 2012年12月8日上午6:27 73 201 我想给它添加“条目”,使它看起来像这样 <user&

我的XML开始时是这样的:

<user>
 <entry>
  <date>December 8, 2012, 6:27 am</date>
  <height>73</height>
  <weight>201</weight>
 </entry>
</user>

2012年12月8日上午6:27
73
201
我想给它添加“条目”,使它看起来像这样

<user>
 <entry>
  <date>December 8, 2012, 6:27 am</date>
  <height>73</height>
  <weight>201</weight>
 </entry>
 <entry>
  <date>December 9, 2012, 6:27 am</date>
  <height>73</height>
  <weight>200</weight>
 </entry>
</user>

2012年12月8日上午6:27
73
201
2012年12月9日上午6:27
73
200
我使用的代码包含第一个
标记中的所有内容。这是我的PHP代码

      $file = 'users/'.$uID.'data.xml';

  $fp = fopen($file, "rb") or die("cannot open file");
  $str = fread($fp, filesize($file));

  $xml = new DOMDocument();
  $xml->formatOutput = true;
  $xml->preserveWhiteSpace = false;
  $xml->loadXML($str) or die("Error");

  // original
  echo "<xmp>OLD:\n". $xml->saveXML() ."</xmp>";

  // get document element
  $root   = $xml->documentElement;
  $fnode  = $root->firstChild;

  //add a node
  $ori    = $fnode->childNodes->item(3);

  $today = date("F j, Y, g:i a");

  $ydate     = $xml->createElement("date");
  $ydateText = $xml->createTextNode($today);
  $ydate->appendChild($ydateText);

  $height     = $xml->createElement("height");
  $heightText = $xml->createTextNode($_POST['height']);
  $height->appendChild($heightText);

  $weight     = $xml->createElement("weight");
  $weightText = $xml->createTextNode($_POST['weight']);
  $weight->appendChild($weightText);

  $book   = $xml->createElement("entry");
  $book->appendChild($ydate);
  $book->appendChild($height);
  $book->appendChild($weight);

  $fnode->insertBefore($book,$ori);
  $xml->save('users/'.$uID.'data.xml') or die("Error");
$file='users/'.$uID.data.xml';
$fp=fopen($file,“rb”)或die(“无法打开文件”);
$str=fread($fp,filesize($file));
$xml=newdomdocument();
$xml->formatOutput=true;
$xml->preserveWhiteSpace=false;
$xml->loadXML($str)或die(“错误”);
//原创的
回显“旧:\n”$xml->saveXML();
//获取文档元素
$root=$xml->documentElement;
$fnode=$root->firstChild;
//添加一个节点
$ori=$fnode->childNodes->item(3);
$today=日期(“fj,Y,g:ia”);
$ydate=$xml->createElement(“日期”);
$ydateText=$xml->createTextNode($today);
$ydate->appendChild($ydateText);
$height=$xml->createElement(“height”);
$heightText=$xml->createTextNode($\u POST['height']);
$height->appendChild($height文本);
$weight=$xml->createElement(“weight”);
$weightText=$xml->createTextNode($_POST['weight']);
$weight->appendChild($weightText);
$book=$xml->createElement(“条目”);
$book->appendChild($ydate);
$book->appendChild($height);
$book->appendChild($weight);
$fnode->insertBefore($book,$ori);
$xml->save('users/'.$uID.data.xml')或die(“Error”);

我如何调整代码,使其将我的条目放在正确的位置?谢谢大家!

您需要将条目附加到根元素,用户:

formatOutput=true;
$xml->preserveWhiteSpace=false;
$xml->loadXML($str)或die(“错误”);
//原创的
回显“旧:\n”$xml->saveXML();
//获取文档元素
$root=$xml->documentElement;
//添加一个节点
$today=日期(“fj,Y,g:ia”);
$ydate=$xml->createElement(“日期”);
$ydateText=$xml->createTextNode($today);
$ydate->appendChild($ydateText);
$height=$xml->createElement(“height”);
$heightText=$xml->createTextNode($\u POST['height']);
$height->appendChild($height文本);
$weight=$xml->createElement(“weight”);
$weightText=$xml->createTextNode($_POST['weight']);
$weight->appendChild($weightText);
$book=$xml->createElement(“条目”);
$book->appendChild($ydate);
$book->appendChild($height);
$book->appendChild($weight);
$root->appendChild($book);
$xml->save('users/'.$uID.data.xml')或die(“Error”);
?>

谢谢!回答得很好。效果很好。