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 5968217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:03:26+00:00 2026-05-22T20:03:26+00:00

I am dummy to PHP and XML, so please be patient if my question

  • 0

I am dummy to PHP and XML, so please be patient if my question seems dumb.

I want to know how to index the XML elements so that I can access them. I am planning to put them into an array. However, I don’t know how to get the number of elements returned.

Here are the codes:

exer.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<actionstars>
 <name>Jean Claude Van Damme</name>
 <name>Scott Adkins</name>
 <name>Michael Jai White</name>
 <name>Dolph Lundgren</name>
 <name>Tom Cruise</name>
 <name>Michael Worth</name>
</actionstars>

index.php

<?php
 $dom = new DomDocument();
 $dom->load("exer.xml");
 $names = $dom->getElementsByTagName("name");
 echo count($names);
 foreach($names as $name) {
     print $name->textContent . "<br />";
 }
?>

When I do the echo count($names); it returns 1, which is obviously not the number of elements. Please help.

  • 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-05-22T20:03:27+00:00Added an answer on May 22, 2026 at 8:03 pm

    Have a look at the return value of getElementsByTagName, which will be a DOMNodeList.

    Also for your problem you could do something like:

    $names = array();
    foreach ($dom->getElementsByTagName("name") as $nameNode) {
      $names[] = $nameNode->nodeValue;
    }
    

    You don’t have to actually check the return value of getElementsByTagName, for it will allways be a DOMNodeList. This way you can use it directly in the foreach-loop whithout assigning unneeded variables.

    What you have to check, is the size of $names, after the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that you can use a dummy int parameter on operator++ and operator--
I want to access the index 'memo' in the associative array in PHP below
Has anyone written a Fast Algorithm that generates a LARGE dummy file in PHP,
I need to insert dummy data on test environtment so that I can run
I am total dummy to programming, php, and cakephp altogether so pls be patient
I want to have use something like this: http://localhost/test/dummy instead of this: http://localhost/test/profile.php?id=dummy I
I know this might sound as really dummy question, but I'm trying to ensure
i am sorry for the dummy question. Here is my simple PHP form with
Image Dummy = Image.FromFile(image.png); Dummy.Save(image.bmp, ImageFormat.Bmp); what the question says i have these using
I have a dummy class where I am testing arrays. I've noticed that when

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.