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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:41:36+00:00 2026-05-31T18:41:36+00:00

I have an element that have items and a tag. The element can contain

  • 0

I have an element that have items and a tag. The element can contain more elements which also have items and a tag, and those elements can also have items and a tag. Beforehand I don’t know how many ‘nested’ elements are in the first element.

I want to save the tag and items in a dictionary with the element as key, and if the element has a ‘nested’ element I want to save that that element and its information in the same dictionary entry as its ‘parent element’.

The code that I have so far works only if the first element has a nested element, and the nested element does not have another nested element in it. This is the code:

def getAllNestedElementInformation(element, nestedDict=None):
    infoDict = {'tagName':element.tag}
    infoDict.update(getItems(element))

    if nestedDict == None:
        nestedDict = infoDict
    else:
        nestedDict['nestedElement'][element] = infoDict
        print nestedDict

    for nestedElement in element:
        nestedDict.update({'nestedElement':collections.defaultdict(dict)})
        getAllNestedElementInformation(nestedElement, nestedDict=nestedDict)

I want to have

 {'name':'scan', 'nestedElement':{'name':'scanwindow', 'nestedElement':{'name:'cvParam'}}}

but I don’t come further than

{'name':'scan', 'nestedElement':{'name:'scanwindow'}}

because I don’t know how to map the next nestedElement in the the ‘nestedElement’ dict.

To give an example of what I have now and what I want to have, when I print nestedDict I get:

{'index': '0', 'nestedElement': {<Element 'scan' at 0x8068180>: {'tagName': 'scan'}}, 'tagName': 'spectrum', 'id': '1'}
{'index': '0', 'nestedElement': {<Element 'cvParam' at 0x80682a0>: {'name': 'scan start time', 'unitName': 'second', 'tagName': 'cvParam', }}, 'tagName': 'spectrum', 'id': '1'}

And I want to have:

{'index': '0', 'nestedElement': {<Element 'scan' at 0x8068180>: 'tagName': 'scan', 'nestedElement: {'name': 'scan start time', 'unitName': 'second', 'tagName': 'cvParam', }}, 'tagName': 'spectrum', 'id': '1'}
  • 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-31T18:41:37+00:00Added an answer on May 31, 2026 at 6:41 pm

    Had to save the return value of getAllNestedElementInformation in a dictionary. Just incase someone want to know: this is what I changed my code into:

    def getAllNestedElementInformation(element):
        infoDict = collections.defaultdict(dict)
        infoDict[element] = {'tagName':element.tag}
        infoDict[element].update(getItems(element))
        if element.text != None:
            if element.text.strip() != '':
                infoDict[element].update({'content':element.text})
    
        for nestedElement in element:
            infoDict[element]['nestedElement'] = getAllNestedElementInformation(nestedElement)  
    
        return infoDict 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an element that is similar to a tag (because it has word-wrap:
I have an element that can have multiple types (not my design). The element
I have a script that animates an element as follows: var item_height = $('#item').height();
If have an element that maybe disabled on a page, and would like to
I want to have a element that displays in the multiple selection display style
I have a form element that contains multiple lines of inputs. Think of each
I have a form element that I want to address via javascript, but it
I have a div element that has an id and this div contains a
I have a button element that I've created like so: $submit = new Zend_Form_Element_Button('submit');
I have trouble getting a form element value into a jstl tag. I am

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.