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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:10:46+00:00 2026-06-16T07:10:46+00:00

Sorry if the question is obscure but this example xml tells it completely. <Components>

  • 0

Sorry if the question is obscure but this example xml tells it completely.

<Components>
    <Component>
        <ComponentID>B1</ComponentID>
        <ComponentName>replace</ComponentName>
        <Description>replace</Description>
    </Component>
    <Component>
        <ComponentID>D2</ComponentID>
        <ComponentName>Red</ComponentName>
        <Description>Red</Description>
    </Component>
    <Component>
        <ComponentID>D3</ComponentID>
        <ComponentName>Yellow</ComponentName>
        <Description>Yellow</Description>
    </Component>
</Components>

Essentially I want to save each of these components to a dictionary. I have tried this code:

if elem1.tag == 'Components':
    for elem2 in list(elem1):
        if elem2.tag == 'Component':
            temp = 0
            for elem3 in list(elem2):
                if elem3.tag == 'ComponentID':
                    temp+=1
                    asset['CompID'+str(temp)] = elem3.text
            for elem3 in list(elem2):
                if elem3.tag == 'ComponentName':
                    temp+=1
                    asset['CompName'+str(temp)] = elem3.text
            for elem3 in list(elem2):
                if elem3.tag == 'Description':
                    temp+=1
                    asset['Description'+str(temp)] = elem3.text

But the issues that happen is that even though each dictionary key will be unique, it is still only the last found component that information will be obtained.

Current output:

{'CompID1': 'D3', 'CompName2': 'Yellow', 'Description3': 'Yellow'}

Desired output:

{'CompID1': 'B1', 'CompName1': 'replace', 'Description1': 'replace',
'CompID2': 'D2', 'CompName2': 'Red', 'Description2': 'Red',
'CompID3': 'D3', 'CompName3': 'Yellow', 'Description3': 'Yellow'}

I know my temp calls and allocation may be wrong, but it is the assigning of the text values within each tag that is the main problem as it only finds the last one. The final solution will need dictionaries at the end for use in other methods and functions. So while any tips and solutions are welcomed, if they can be focused on dictionaries that would be very helpful.

  • 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-06-16T07:10:48+00:00Added an answer on June 16, 2026 at 7:10 am

    Try to change it:

    if elem1.tag == 'Components':
        temp = 0
        for elem2 in list(elem1):
            if elem2.tag == 'Component':
                for elem3 in list(elem2):
                    if elem3.tag == 'ComponentID':
                        asset['CompID'+str(temp)] = elem3.text
                    if elem3.tag == 'ComponentName':
                        asset['CompName'+str(temp)] = elem3.text
                    if elem3.tag == 'Description':
                        asset['Description'+str(temp)] = elem3.text
                temp+=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this question seems stupid, but it's stumped me for a couple days,
Sorry if this question is a bit open ended, but I'm pretty new to
Sorry for this question. There is a lot of information but unfortunately I haven't
Sorry if this question seems obvious for everyone, but I am very new to
Sorry - my question is almost identical to this one but since it didn't
Sorry basic question I'm sure but I can't seem to figure this out. Say
This is an absolute beginner's question (sorry), but I was wondering how to commit
Sorry for question but I can't find answer anywhere on internet. I couldn't find
Quick question (and sorry if it's already been asked, I have looked but couldn't
Sorry for newbie question but is there a way to import someone else commit

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.