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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:44:43+00:00 2026-05-17T02:44:43+00:00

I’m using PHP’s DOMDocument and related classes to work with XML. The XML contains

  • 0

I’m using PHP’s DOMDocument and related classes to work with XML. The XML contains processing instructions that must be processed in a specific order, from top to bottom, deepest to shallowest.

I’m using a recursive function that takes the entire DOMDocument, then calls itself foreach child node, all the way down.

I need to delete some of these child nodes after I process them. The problem is that as soon as a child node is deleted, it seems the foreach is “out of whack” and I can’t delete any more child nodes.

Example XML doc:

<root id="1">
    <instruction id="2">
        <inner id="3"/>
    </instruction>
    <instruction id="4">
        <inner id="5"/>
    </instruction>
</root>

I need to process inner[@id=3] first, then delete it. Then I need to process instruction[id=2] and delete it. Up until here, everything’s fine.

Next I need to process inner[@id=5] and delete it. I can read/process it ok, but when I try to remove it:

$parentNode=$inner->parentNode;
$parentNode->removeChild($inner);

Nothing happens. It seems that removing the first instruction node has made PHP confused about which elements are contained in the document now.

I know it’s possible to process the XML from bottom to top and remove all of the nodes in that order, but I have a specific need to go from top-to-bottom.

One other piece of info here – I’m also adding some new nodes to the document while processing it, in case that changes my options.

What do I need to do to get this to work?

  • 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-17T02:44:44+00:00Added an answer on May 17, 2026 at 2:44 am

    A lot of the DOM structures are internally just arrays. Deleting elements will shift things around as the keys/pointers stored in returned NODElist objects will refer to array offsets which no longer exist, or contain something completely differnt.

    To properly handle deleting DOM nodes without upsetting these various pointers, you have to work from the end of the array towards the beginning. Instead of deleting the nodes in-place, store them in a stack-type structure and after you’ve completed operations on any particular level of the tree, you can pop everything off the stack and do the deletions then. At this point any dangling pointers will be moot as you’ve completed your work already.

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

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.