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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:35:25+00:00 2026-06-13T22:35:25+00:00

I have a program that will take input xml and print same xml as

  • 0

I have a program that will take input xml and print same xml as output. How can i implement class and method for that ?

import xml.etree.ElementTree as ET
import sys   
doc       = ET.parse("users.xml")
root      = doc.getroot()
root_new  = ET.Element("users") 
for child in 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 for skipping KeyError
    if 'remote_access' in child.attrib:
        remote_access   = child.attrib['remote_access']
        user.set("remote_access",remote_access) 
    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(sys.stdout)

How to convert this to class concept . 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-13T22:35:26+00:00Added an answer on June 13, 2026 at 10:35 pm

    If I understood you correctly, there are probably thousands of ways to do it.
    You want an example, here it is (I’m assuming your code is working):

    import xml.etree.ElementTree as ET
    import sys   
    
    
    class MyXmlParser(object):
    
        def __init__(self, xml_file_name):
            self.doc  = ET.parse(xml_file_name)
            self.root = 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)
    
    def main():
        my_parser = MyXmlParser("users.xml")
        my_parser.do_something()
    
    if __name__ == '__main__':
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a program that will take in an image as input,
I have several scripts that take as input a directory name, and my program
I have a program below that tries to take input from the user and
I want to create a program that will take user input and create a
I am trying to make a program that will take as input a string
My goal is to produce a program that will take a file as input
I am trying to write a program that will take the users input value
I am trying to create a program that will take user inputs(marks) and output
I have a program that will let me manage users on our terminal server
I have the below program that will move files from one directory to other.

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.