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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:44:47+00:00 2026-05-16T12:44:47+00:00

What are my options if I want to create a simple XML file in

  • 0

What are my options if I want to create a simple XML file in python? (library wise)

The xml I want looks like:

<root>
 <doc>
     <field1 name="blah">some value1</field1>
     <field2 name="asdfasd">some vlaue2</field2>
 </doc>

</root>
  • 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-05-16T12:44:48+00:00Added an answer on May 16, 2026 at 12:44 pm

    These days, the most popular (and very simple) option is the ElementTree API,
    which has been included in the standard library since Python 2.5.

    The available options for that are:

    • ElementTree (Basic, pure-Python implementation of ElementTree. Part of the standard library since 2.5)
    • cElementTree (Optimized C implementation of ElementTree. Also offered in the standard library since 2.5. Deprecated and folded into the regular ElementTree as an automatic thing as of 3.3.)
    • LXML (Based on libxml2. Offers a rich superset of the ElementTree API as well XPath, CSS Selectors, and more)

    Here’s an example of how to generate your example document using the in-stdlib cElementTree:

    import xml.etree.cElementTree as ET
    
    root = ET.Element("root")
    doc = ET.SubElement(root, "doc")
    
    ET.SubElement(doc, "field1", name="blah").text = "some value1"
    ET.SubElement(doc, "field2", name="asdfasd").text = "some vlaue2"
    
    tree = ET.ElementTree(root)
    tree.write("filename.xml")
    

    I’ve tested it and it works, but I’m assuming whitespace isn’t significant. If you need "prettyprint" indentation, let me know and I’ll look up how to do that. (It may be an LXML-specific option. I don’t use the stdlib implementation much)

    For further reading, here are some useful links:

    • API docs for the implementation in the Python standard library
    • Introductory Tutorial (From the original author’s site)
    • LXML etree tutorial. (With example code for loading the best available option from all major ElementTree implementations)

    As a final note, either cElementTree or LXML should be fast enough for all your needs (both are optimized C code), but in the event you’re in a situation where you need to squeeze out every last bit of performance, the benchmarks on the LXML site indicate that:

    • LXML clearly wins for serializing (generating) XML
    • As a side-effect of implementing proper parent traversal, LXML is a bit slower than cElementTree for parsing.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse a large XML file and I have two options: Perl
I want to create a simple file upload server. The server should be able
I want to create a xml file from the data given by the users.
I want to create a set of options, that will be appended to every
I want to print my whole file compilation options on make in my console
I want to create a simple bookmarklet to pass some information from javascript to
I want to create a simple graphical user interface to allow non-technical users to
I have a very simple XML datasource structured like this: <datasource> <row> <column>Some text
I want to create a simple menu in C program that accepts single character.
Hello This is a general question.I want to create a simple report just some

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.