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

  • Home
  • SEARCH
  • 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 9104405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:51:39+00:00 2026-06-17T01:51:39+00:00

This question may seem very stupid, but I am not able to find much

  • 0

This question may seem very stupid, but I am not able to find much help on how to find the node value of the last child using PHP, even though it’s a piece of cake with JS.
This is what my XML currently looks like:

<?xml version="1.0"?>
<files>
 <file>.DS_Store</file>
 <file>ID2PDF_log_1.xml</file>
 <file>ID2PDF_log_12.xml</file>
 <file>ID2PDF_log_15.xml</file>
</files>

Here’s the php code:

$filename = 'files.xml'; //my xml file name
$dom = new DomDocument();
$dom->load($filename);

$elements = $dom->getElementsByTagName('file');
echo $elements->lastChild(); // This is obviously not working
/*I get an error that I am trying to access an undefined method in DOMNodeList. Now, I know
that lastChild is a property of DOMNode. But I can't figure out how I can change my code to 
get this to work.*/  

I am trying to echo out

ID2PDF_log_15.xml

Can anyone show me how to get this done?

P.S.: I don’t want to change the xml file structure because I am creating it through a script and I am a lazy programmer. But, I did do my research to get this. Didn’t help.

I did try getting the number of elements in the node ‘file’ and then using item(#), but that didn’t seem to work either.

Thanks

SOLUTION

$filename = 'files.xml';
$dom = new DomDocument();

$dom->load($filename);

$elements = $dom->getElementsByTagName('file')->length;
echo 'Total elements in the xml file:'.$elements."\n";

$file = file_get_contents('files.xml');
$xml = simplexml_load_string($file); 

$result = $xml->xpath('file');
echo "Last element".$result[$elements-1]."\n";

I’ll make this neater a little later. But, just thought that I should share the answer anyway any new users in the future.

  • 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-17T01:51:40+00:00Added an answer on June 17, 2026 at 1:51 am

    This should work:

    $elements->xpath('root/child[last()]');
    

    Read up about xpath

    Alternatively I would suggest counting the number of elements, and then targeting the last element using that count:

    $file_count = $elements->getElementsByTagName('file')->length;
    $elements[$file_count];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem like a very basic question but I did not seem to
This may seem to be an academic question, but still I would be very
This may seem like a basic/stupid/obviously-answered question, but I wanted to check: why use
I am still learning SQL so this may seem a very odd question, but
this may seem like a very simple question, but I want to dump some
This may be a very dumb question but I can't seem to get it
This is a very simple question, but I haven't seem to be able to
This question may seem like a novice, and perhaps 'stupid' question but please bear
This may seem like a very simple question, but I have been struggling with
Ok this may seem like a very, very awkward question but here's my problem:

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.