2

My XML starts off looking like this:

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

I want to add "entries" to it so that it would look like this

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

My code I am using encloses everything within the first <entry>...</entry> tags. Here is my PHP code.

      $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");

How can I adjust my code so it puts my entries in the correct place? Thank you!

1 Answer 1

3

You need to append entry to the root element, user:

<?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");
?>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.