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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:58:22+00:00 2026-06-13T21:58:22+00:00

I have the following Python program: import xml.etree.ElementTree as ET import sys class MyXmlParser(object):

  • 0

I have the following Python program:

 import xml.etree.ElementTree as ET
    import sys   

    class MyXmlParser(object):

        def __init__(self, users):
            self.doc  = ET.parse("users.xml")
            self.root = self.doc.getroot()

        def do_something(self, output = sys.stdout):
            root_new  = ET.Element("users") 
            for child in self.root:
                username             = child.attrib['username']
                password             = child.attrib['password']   
                # create "user" here
                user    = ET.SubElement(root_new, "user") 
                user.set("username",username)               
                user.set("password",password) 
                # checking attribute - skip KeyError
                try:
                    remote_access   = child.attrib['remote_access']
                    user.set("remote_access", remote_access) 
                except KeyError:
                    pass

                for g in child.findall("group"):
                    # create "group" here
                    group     = ET.SubElement(user,"group")  
                    if g.text != "lion":
                        group.text = g.text 
            tree = ET.ElementTree(root_new)
            tree.write(output)
if __name__ =='main':
    parser = MyXmlParser()
    parser.do_something()

And this is my xml :

<users>
<user username="admin"  password="admin" remote_access="yes"></user>
<user username="private_user1" password="user1" ><group>group1</group><group>group2</group></user>
<user username="private_user2" fullname="user2" password="user2"><group>group1</group><group>group2</group></user>
</users>

I can’t find any mistake in this program. It should output some xml. But presently it’s not giving any error or output. I expected some xml as output 🙁 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-13T21:58:23+00:00Added an answer on June 13, 2026 at 9:58 pm

    I changed your MyXmlParser class constructor to:

        def __init__(self, users=None):
            self.doc = ET.parse("users.xml")
            self.root = self.doc.getroot()
    

    and added this after the class definition:

    if __name__==" __main_ ":
        parser = MyXmlParser()
        parser.do_something()
    

    and got this one line of output:

    <users><user password="admin" remote_access="yes" username="admin" /><user password="user1" username="private_user1"><group>group1</group><group>group2</group></user><user password="user2" username="private_user2"><group>group1</group><group>group2</group></user></users>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Python 2.6 program and YAML definition (using PyYAML ): import
i have the following code in a module called code_database.py class Entry(): def enter_data(self):
I have this following program in Python. import re data = '''component FA_8 is
I have the following code in python from selenium import webdriver from selenium.webdriver.common.by import
I have the following in my python script.. hostIP = sys.argv[1] hostsFileLoc = hosts.txt
In python I have the following function: def is_a_nice_element(element, parameter): #do something return True
I have the following code: #!/usr/bin/python # -*- coding: iso-8859-15 -*- import pygame, random
I am a newbie in Python. I have written following program and expecting all
I have the following code in Python import subprocess import time info = subprocess.STARTUPINFO()
I have the following python script which takes some inputs and puts them in

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.