Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8956445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:47:19+00:00 2026-06-15T14:47:19+00:00

My XML starts off looking like this: <user> <entry> <date>December 8, 2012, 6:27 am</date>

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:47:19+00:00Added an answer on June 15, 2026 at 2:47 pm

    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");
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take an xml like this: <?xml version=1.0?> <events> <event> <start></start> <duration></duration> <begin>{somecallback}</begin> <complete>{someclass.someproperty =
When a JSF/XPages application starts it reads the faces-config.xml for managed beans, validators etc.
I've been looking around a bit for how to get the time off of
I am trying to write an application that starts off from a main menu
I'm having major difficulties to start off with NHiberante. Main problems: Where my hbm.xml
I have an XML file that I want to transform using an XSLT. It
So I am grabbing this XML data, and parsing it within the success function
First off, I'm not terribly experienced in XML. I know the very basics of
I might be really off on this one but can anyone look over this
I have a large XML data file (>160M) to process, and it seems like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.