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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:49:31+00:00 2026-06-13T20:49:31+00:00

I like to create a nodes every time when i go through a loop.But

  • 0

I like to create a nodes every time when i go through a loop.But presently only the last value of the loop will be used. How can i achieve this using python. following is my example.

My xml :-

<person>
<user name="david" password="super"></user>
<user name="alen" password="boss"></user>
<user name="windeesal" password="sp"></user>
</person>

The python code:

import xml.etree.ElementTree as ET

doc = ET.parse("users.xml")
root = doc.getroot() #Returns the root element for this tree.
root.keys()          #Returns the elements attribute names as a list. The names are returned in an arbitrary order
for child in root:
    name = child.attrib['name']
    password = child.attrib['password']

root = ET.Element("person")
user = ET.SubElement(root, "user")
user.set("username",username)
user.set("password",password)

tree = ET.ElementTree(root)
myxml = tree.write("new.xml")

print myxml 

Out put of the code contain only last value of loop 🙁

<person>
<user password="sp" username="windeesal" />
</person>

how to create the nodes every time i go through the loop then join the results and write them to the file .? am really a beginner please give me a detail explanation. Thank you very much .

  • 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-13T20:49:31+00:00Added an answer on June 13, 2026 at 8:49 pm

    Try next one. Your understanding of python seems to be very basic so I am not sure what to write about problem.

    Please ask if you need explanation! 🙂

    import xml.etree.ElementTree as ET
    
    doc    = ET.parse("users.xml")
    root = doc.getroot() #Returns the root element for this tree.
    root_new  = ET.Element("person") 
    for child in root:
        name                = child.attrib['name']
        password             = child.attrib['password']
    
        user  = ET.SubElement(root_new, "user") # create subelement in cycle! 
        user.set("username",name)               # username variable is not declared
        user.set("password",password)
    
    tree = ET.ElementTree(root_new)
    tree.write("new.xml")
    
    import sys
    tree.write(sys.stdout)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have two tables structured like so: CREATE TABLE #nodes(node int NOT NULL);
I'm terribly sorry for asking yet another anonymous-function question, but it seams like every
I'm trying to search through my inbox to find specific email's but there every
I'm trying to create a basic Node application, every time a client connects I
I would like to create a child process in node, and block in a
I'd like to create a simple site on NodeJS. For example, it has two
I am trying to create a tree like node diagram, like the example image
I am attempting to create a shell script using Node.js similar to packages like
Given a table like: CREATE TABLE MyTable ( MyColumn NUMBER NOT NULL ); I
Say you create a directory like: CREATE OR REPLACE DIRECTORY EXT_DATA_FILES AS '/data/ext_data_files'; GRANT

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.