php - 使用 PHP 添加 XML 节点

标签 php xml nodes

我的 XML 开始看起来像这样:

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

我想向其中添加“条目”,使其看起来像这样

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

我正在使用的代码将所有内容都包含在第一个 <entry>...</entry> 中标签。这是我的 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");

如何调整我的代码,以便将我的条目放在正确的位置?谢谢!

最佳答案

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

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

//add a node
$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);

$root->appendChild($book);
$xml->save('users/'.$uID.'data.xml') or die("Error");
?>

关于php - 使用 PHP 添加 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13775057/

相关文章:

php - 使用 php 将我的 sql 数据库上传到我的 html 表单中

php - Ajax更新、比较和插入数据到mysql

xml - 对 perl 的 Entities.pm 感到困惑

graphviz - 获取 graphviz 以在边缘上方绘制节点

php - Opencart - 基于名称和类型的自定义选项

javascript - 刷新留言箱

xml - Emacs 匹配标签高亮显示

java - 如何通过电子邮件将 xslt 报告发送给 xls 文件中给出的用户列表?

javascript - 创建一个 while 循环,在一定的尝试后中断 - NodeJS

java - 链表循环潜在异常