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

The Archive Base Latest Questions

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

I have a program,But that not contain class concept ( Python program follow some

  • 0

I have a program,But that not contain class concept ( Python program follow some value to class concept) am really a new one in python world. So learning from the original way help me out to shine in this world. Can anyone kindly help me out, instead of making this question negative mark 🙁

 import xml.etree.ElementTree as ET
 import sys

doc       = ET.parse("books.xml")
root      = doc.getroot() 
root_new  = ET.Element("books") 
for child in root:
    name                = child.attrib['name']
    cost                = child.attrib['cost']
    # create "book" here
    book    = ET.SubElement(root_new, "book") 
    book.set("name",name)               
    book.set("cost",cost) 
    if 'color' in child.attrib:
        color               = child.attrib['color']
        book.set("color",color) 
    if 'weight' in child.attrib:
        weight              = child.attrib['weight']
        book.set("weight",weight)
    for g in child.findall("cover"):
        # create "group" here
       cover     = ET.SubElement(cover,"cover")  
        if g.text != "goldcover":
            cover.text = g.text 
tree = ET.ElementTree(root_new)
tree.write(sys.stdout)

For understanding : my xml is ,

<books>
<book name="goodbook" cost="10" color="green"></book>
<book name="badbook" cost="1000" weight="100"><cover>papperback</cover><cover>hardcover</cover></book>
<book name="avgbook" cost="99" weight="120"></book>
</books>

As a fresher on python i hope some one will help me out , All valuable input are warmly welcomed.

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

    Ok, this is not that hard of an exercise but I would go about this this way. You have a collection of books thus my class would be called BookCollection and it would take in the path to a XML file.

    Now, what you need is the following a method to parse the XML, a method to get a book, and a method to set a book. So, a skeleton class would look like:

    class BookCollection( object ):
        def __init__( self, xml_path ):
            """call the parse with the xml_path here"""
            self.bookList = []#This is a list of tuples
    
        def _parse( self, xml_path ):
            """This method is private and only parses the 
               xml and stores the books as tuples in a list"""
    
        def get( self, title ):
            """This method allows the user of this class to get 
               a book from the list of tuples"""
    
        def _set( self, title, cost, weight, cover=None ):
            """This method sets and adds a book tuple to the 
               list of book tuples"""
    

    You do not need to represent a book as a class in python as that just over complicates the entire bit of a code thus the tuple. And, why I abstracted the XML file as a book collection.

    I will not fill in the details beyond the skeleton so you can learn the rest from there.

    EDIT:
    As it appears you want to also output the XML I would add a to_xml method to the above class that will write out your XML. If you need to remove books I would also add the associated method, but that is up for your to implement.

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

Sidebar

Related Questions

We have a program with a main() that parses certain CLPs but does not
I have program that requires Python 3, but I develop Django and it uses
I'm almost finished with my program, but I have one problem that I can't
I have a program that is developed in MVC 3 Razor Syntax but whenever
I have a program that encrypts files, but adds the extension .safe to the
I have a program that creates semaphore. But when i try to use SETALL,
I am basically doing a tutorial but my program seems to have errors that
I have a series of big excel files that work like a program, but
I have a COM Callable Wrapper that I'm using from a VB6 program, but
my host (one.com) doesn´t support java webapps. But I have a java programm that

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.