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.
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):