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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:48:17+00:00 2026-05-23T19:48:17+00:00

I am attempting to manipulate a DOM tree using lxml ‘s etree module. One

  • 0

I am attempting to manipulate a DOM tree using lxml‘s etree module. One task I haven’t figured out yet is how to test whether a particular node is still part of a parsed tree. Since the behavior of etree is mostly undefined if you remove nodes during _ElementTree.iter(), I do the manipulation in two phases.

First, I iterate through the parsed tree and mark some nodes for removal and certain other nodes for further processing by placing them in respective lists. The second phase consists of iterating through the list of nodes to remove and removing them from the tree. At this point, I have a list of nodes to process further and a tree that has been pruned substantially since it was first parsed.

What I lack is a way to test if a particular node in my nodes-to-process list still lives in the parse tree. If it isn’t part of the tree, that means it’s a descendant of one of the nodes I removed earlier and I want to discard it. The problem is that there isn’t an obvious way to do this test cheaply. Even after a node has been removed from the _ElementTree calling getroottree() on that node returns the original tree.

I could call iterancestors() on each node-to-process and check for the root element I would expect for an in-tree node, but this is O(n) and won’t scale well for deep DOM trees.

Does anyone know of a constant time operation, given an Element and an _ElementTree, to test whether the former is part of the latter?

I realize that traversing a node’s parent chain upward might be the only way to do this test, and any faster way would require some bookkeeping to be implemented by the library.

  • 1 1 Answer
  • 1 View
  • 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-23T19:48:18+00:00Added an answer on May 23, 2026 at 7:48 pm

    Step 0: parse the xml into a tree.
    Step 1: iterate over the tree, deleting nodes that need to be deleted.
    Step 2: iterate over the remaining nodes, processing those that need it.

    If you own step 0, you can use iterparse() with end events to save building a large tree only to remove many nodes later, and making step 1 much simpler:

    for event, elem in etree.iterparse(input_xml):
        if elem needs deleting:
            elem.clear() # remove text, tail, attributes, and descendant elements
            delete_todo.append(elem)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempting to print out a list of values from 2 different variables that are
Attempting to set up Samba + OpenLDAP using nss_ldap. After joining Windows7 to Samba
I am attempting to manipulate an asp.net MVC2 view variable in Jquery... (function ()
I was initially attempting visualize a 4 parameter function with Plot3D and Manipulate sliders
I'm building a rails3 app on heroku, and I'm using aws-s3 gem to manipulate
I am attempting to put together a fairly complex form using dojo and dijit
I'm using BeanUtils to manipulate Java objects created via JAXB, and I've run into
Attempting to learn from doing an implementation of Quicksort, I cannot find out why
I am attempting to manipulate an Adobe Reader plist file on OS X to
Attempting to build .exe using py2exe: python setup.py py2exe , receiving error: ImportError: No

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.