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

  • Home
  • SEARCH
  • 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 8768279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:01:38+00:00 2026-06-13T17:01:38+00:00

import xml.etree.ElementTree as ET doc = ET.parse(users.xml) root = doc.getroot() #Returns the root element

  • 0
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:
    username             = child.attrib['username']
    password             = child.attrib['password']
    grant_admin_rights   = child.attrib['grant_admin_rights']
    create_private_share = child.attrib['create_private_share']
    uid                  = child.attrib['uid']



root  = ET.Element("users")
user  = ET.SubElement(root, "user")
user.set("username",username)
user.set("password",password)
user.set("grant_admin_rights",grant_admin_rights)
user.set("create_private_share",create_private_share)
user.set("uid",uid)

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

print myxml 

my input xml :-

<users>
<user username="admin" fullname="admin" password="admin" create_private_share="yes" remote_access="yes" grant_admin_rights="yes" enable_encrypt_share="yes" volume="DataVolume5" uid="1000"></user>
<user username="os" fullname="sp" password="sp" grant_admin_rights="no" create_private_share="no" uid="1000" ></user>
<user username="us" fullname="sp" password="sp" grant_admin_rights="no" create_private_share="no" uid="1000" ></user>
</users>

Out put of this code :

<user create_private_share="no" grant_admin_rights="no" password="sp" uid="1000" username="us" />

i trying for to get the exact xml as my input xml , but presently it iterating all child but output only coming, the last ? how can i iterate all child and print the exact xml .

in my case print also not working it showing None.

I would like my output should liek this :

<users>
<user username="admin" fullname="admin" password="admin" create_private_share="yes" remote_access="yes" grant_admin_rights="yes" enable_encrypt_share="yes" volume="DataVolume5" uid="1000"></user>
<user username="os" fullname="sp" password="sp" grant_admin_rights="no" create_private_share="no" uid="1000" ></user>
<user username="us" fullname="sp" password="sp" grant_admin_rights="no" create_private_share="no" uid="1000" ></user>
</users>

Thanks in advance 🙂

  • 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-13T17:01:40+00:00Added an answer on June 13, 2026 at 5:01 pm

    You have some problems in logic. Only the last value of the loop will be used. You should try to create the nodes every time you go through the loop then join the results and write them to the file

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

Sidebar

Related Questions

import xml.etree.ElementTree as ET doc = ET.parse(users.xml) root = doc.getroot() #Returns the root element
import xml.etree.ElementTree as ET xmldata = file('my_xml_file.xml') tree = ET.parse(xmldata) root = tree.getroot() root_iter
This is my piece of code: #!/usr/bin/python from xml.etree.ElementTree import ElementTree from array import
please consider this code: import xml.etree.ElementTree as ET import urllib XML_response = urllib.urlopen('http://www.navlost.eu/aero/metar/?icao=LWSK&dt0=2011-05-03+12%3A00%3A00&c=1&rt=metar').read() tree
I parse in the XML using from lxml import etree tree = etree.parse('test.xml', etree.XMLParser())
I have been trying to parse a file with xml.etree.ElementTree : import xml.etree.ElementTree as
import os import xml.etree.ElementTree as et for ev, el in et.iterparse(os.sys.stdin): el.clear() Running the
I am having the below code. import xml.dom.minidom def get_a_document(name): return xml.dom.minidom.parse(name) doc =
import urllib import xml.etree.ElementTree as ET def getWeather(city): #create google weather api url url
I am trying to use xml.etree.ElementTree to parse responses from eBay finding API, findItemsByProduct.

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.