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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:14:11+00:00 2026-05-26T08:14:11+00:00

I am using DOMDocument class in PHP to remove all the child nodes of

  • 0

I am using DOMDocument class in PHP to remove all the child nodes of ‘body’ element. My code is as follows

$doc=new DOMDocument();
$doc->loadHTMLFile("a.html");

$wrapperDiv=$doc->createElement('div');
$wrapperDiv->setAttribute('class','wrapper');

$body= $doc->getElementsByTagName('body')->item(0);
foreach( $body->childNodes as $child)
{

        if($child->nodeName != "#text")
        {
            $wrapperDiv->appendChild($child);
            $body->removeChild($child);
        }

} 

$body->appendChild($wrapperDiv);
$doc->saveHTMLFile('aaa.html');

at $body->removeChild($child); it gives me error

Uncaught exception ‘DOMException’ with message ‘Not Found Error’ in
C:\xampp\htdocs\test\dum2.php:70 Stack trace: #0
C:\xampp\htdocs\test\dum2.php(70):
DOMNode->removeChild(Object(DOMElement)) #1 {main} thrown in
C:\xampp\htdocs\test\dum2.php on line 70

I have been struggling with this for a long time now but could not figure out what the problem is, as I am new to using this DOMDocument class. The ‘body’ element does contain children!

  • 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-26T08:14:12+00:00Added an answer on May 26, 2026 at 8:14 am

    A node can only have one parent. So I assume that when you called $wrapperDiv->appendChild($child);, $child is not a child of $body anymore and hence $body->removeChild($child); throws an error.

    Meaning: You don’t need to remove the child as it is already removed.

    If on the other hand you really want to remove the children and not append them anywhere else, then remove $wrapperDiv->appendChild($child);.

    Update: Indeed it seems that if there are several Element nodes, not all of the nodes are moved: http://codepad.org/8udqSNMj

    To fix this, try to iterate over the child elements in reverse order:

    $children = $body->childNodes;
    for($i = $children->length; $i--;) {
        $child = $children->item($i);
        if($child->nodeName != "#text") {
            $wrapperDiv->appendChild($child);
        }
    } 
    

    http://codepad.org/LtuJN2ZT

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

Sidebar

Related Questions

Is there a way to remove a HTML element by using the DOMDocument class?
i'm using the DOMdocument class in php whenever i want to create a XML
I'm reading some XML with PHP and currently using the DOMDocument class to do
In PHP I'm using DOMDocument and I need to search for an element that
Given the following PHP code using DOMDocument : $inputs = $xpath->query('//input | //select |
I am using php's DOMDocument->getElementById->nodeValue to set a particular DOM element's HTML. The problem
I have the following html and i am using php's DomDocument class to get
For one of my projects, I'm using the DOMDocument class to load and manipulate
I'm using PHP's DOMDocument to parse and normalize user-submitted HTML using the loadHTML method
I'm using the a wrapper around the PHP5-class DOMDocument to generate my HTML. This

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.