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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:56:25+00:00 2026-05-21T18:56:25+00:00

How get first level of dom elements by Domdocument PHP? Example with code that

  • 0

How get first level of dom elements by Domdocument PHP?

Example with code that not works – tooken from Q&A:How to get nodes in first level using PHP DOMDocument?

<?php
$str=<<< EOD
<div id="header">
</div>
<div id="content">
    <div id="sidebar">
    </div>
    <div id="info">
    </div>
</div>
<div id="footer">
</div>
EOD;

$doc = new DOMDocument();
$doc->loadHTML($str);
$xpath = new DOMXpath($doc);
$entries = $xpath->query("/");
foreach ($entries as $entry) {
    var_dump($entry->firstChild->nodeValue);
}
?>
  • 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-21T18:56:26+00:00Added an answer on May 21, 2026 at 6:56 pm

    The first level of elements below the root node can be accessed with

    $dom->documentElement->childNodes
    

    The childNodes property contains a DOMNodeList, which you can iterate with foreach.

    See DOMDocument::documentElement

    This is a convenience attribute that allows direct access to the child node that is the document element of the document.

    and DOMNode::childNodes

    A DOMNodeList that contains all children of this node. If there are no children, this is an empty DOMNodeList.

    Since childNodes is a property of DOMNode any class extending DOMNode (which is most of the classes in DOM) have this property, so to get the first level of elements below a DOMElement is to access that DOMElement’s childNode property.


    Note that if you use DOMDocument::loadHTML() on invalid HTML or partial documents, the HTML parser module will add an HTML skeleton with html and body tags, so in the DOM tree, the HTML in your example will be

    <!DOCTYPE html … ">
    <html><body><div id="header">
    </div>
    <div id="content">
        <div id="sidebar">
        </div>
        <div id="info">
        </div>
    </div>
    <div id="footer">
    </div></body></html>
    

    which you have to take into account when traversing or using XPath. Consequently, using

    $dom = new DOMDocument;
    $dom->loadHTML($str);
    foreach ($dom->documentElement->childNodes as $node) {
        echo $node->nodeName; // body
    }
    

    will only iterate the <body> DOMElement node. Knowing that libxml will add the skeleton, you will have to iterate over the childNodes of the <body> element to get the div elements from your example code, e.g.

    $dom->getElementsByTagName('body')->item(0)->childNodes
    

    However, doing so will also take into account any whitespace nodes, so you either have to make sure to set preserveWhiteSpace to false or query for the right element nodeType if you only want to get DOMElement nodes, e.g.

    foreach ($dom->getElementsByTagName('body')->item(0)->childNodes as $node) {
        if ($node->nodeType === XML_ELEMENT_NODE) {
            echo $node->nodeName;
        }
    }
    

    or use XPath

    $dom->loadHTML($str);
    $xpath = new DOMXPath($dom);
    foreach ($xpath->query('/html/body/*') as $node) {
        echo $node->nodeName;
    }
    

    Additional information:

    • DOMDocument in php
    • Printing content of a XML file using XML DOM
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there way in next piece of code to only get the first record?
This is my first post here and I wanted to get some input from
With :limit in query, I will get first N records. What is the easiest
I need to get the first and last day of a month in the
I'm attempting to get my first ASP.NET web page working on windows using Mono
When debugging in Internet Explorer, I first get an alert box with extremely limited
I need to get just the first item (actually, just the first key) off
First, let's get the security considerations out of the way. I'm using simple authentication
So this question will get technical – eventually – but first check out Hanselminutes
I am taking my first steps programming in Lua and get this error 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.