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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:32:00+00:00 2026-05-11T06:32:00+00:00

I need to load an XML file and convert the contents into an object-oriented

  • 0

I need to load an XML file and convert the contents into an object-oriented Python structure. I want to take this:

<main>     <object1 attr='name'>content</object> </main> 

And turn it into something like this:

main main.object1 = 'content' main.object1.attr = 'name' 

The XML data will have a more complicated structure than that and I can’t hard code the element names. The attribute names need to be collected when parsing and used as the object properties.

How can I convert XML data into a Python object?

  • 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. 2026-05-11T06:32:00+00:00Added an answer on May 11, 2026 at 6:32 am

    It’s worth looking at lxml.objectify.

    xml = '''<main> <object1 attr='name'>content</object1> <object1 attr='foo'>contenbar</object1> <test>me</test> </main>'''  from lxml import objectify  main = objectify.fromstring(xml) main.object1[0]             # content main.object1[1]             # contenbar main.object1[0].get('attr') # name main.test                   # me 

    Or the other way around to build xml structures:

    item = objectify.Element('item') item.title = 'Best of python' item.price = 17.98 item.price.set('currency', 'EUR')  order = objectify.Element('order') order.append(item) order.item.quantity = 3 order.price = sum(item.price * item.quantity for item in order.item)  import lxml.etree print(lxml.etree.tostring(order, pretty_print=True)) 

    Output:

    <order>   <item>     <title>Best of python</title>     <price currency='EUR'>17.98</price>     <quantity>3</quantity>   </item>   <price>53.94</price> </order> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need some help i want to load an xml file into a table
I need to load a .xml file from a URL adress into an NSData
I need to load a xml file present in classpath into my java applicatio.
I want to load an XML file on the internet into a DataTable in
I need to load xml from a file into an XmlDocument. The problem is
I need to modify a (xml-)file from Apache Ant. loadfile task allows to load
I need to load a file from an umounted TrueCrypt disk into memory. Is
I need to load an xML file from the bin folder in ASP.NET (MVC,
I basicly need to load the whole XML file, add a new line with
I load xml file into DOM model and analyze it. The code for 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.