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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:57:21+00:00 2026-05-23T09:57:21+00:00

I’m fairly new to Python, and I’ve just started working with XML parsing. I

  • 0

I’m fairly new to Python, and I’ve just started working with XML parsing. I am getting a bit overwhelmed by all the options for working with XML, and I’m hoping an experienced person can give me some advice (and perhaps a code sample??) for the simple problem I’m working on.

I am working on a simple Python contact management application that does not involve a database – each contact’s information is stored in a separate text file using XML. For example, assume the following is the contents of the file “1234.xml”

<contact>
<id>1234</id>
<name>Johnny Appleseed</name>
<phone>8145551212</phone>
<address>
    <street>1234 Main Street</street>
    <city>Hometown</city>
    <state>OH</state>
</address>
<address>
    <street>1313 Mockingbird Lane</street>
    <city>White Plains</city>
    <state>NY</state>
</address>
</contact>

For sake of example, let’s assume there can be only one phone number, but multiple address blocks.

For what I’m doing here, I need to be able to parse the XML from the file, make changes to the data, and then update the XML and save it back to the file. Let’s assume there are three types of data changes that might occur:

  1. changing the data for one or more items, such as updating a phone number

  2. adding a new address block (and the corresponding data for the street/city/state of the new address)

  3. deleting an existing address block

Given what I’m trying to do here, can you recommend a particular way of doing this? (SAX, DOM, minidom, ElementTree, something else?) Code samples for whatever you suggest would be greatly appreciated.

Thank you!

Ron

  • 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-23T09:57:22+00:00Added an answer on May 23, 2026 at 9:57 am

    The SAX and DOM APIs are older; they were pretty much translated from the Java world into Python. The ElementTree API was designed specifically to be Pythonic, i.e. to fit with the Python way of problem solving, so prefer that.

    The richest and fastest implementation of ElementTree that I know of is lxml. It’s XPath functionality is very useful. Untested example:

    from lxml import etree
    
    contacts = etree.parse(open("1234.xml"))
    
    for c in contacts.xpath('//contact'):
        if c.xpath('/name')[0].text == 'Johnny Appleseed':
            c.xpath('/phone')[0].text = NEW_PHONE_NUMBER
    
    print >> open("1234.xml", "w"), etree.tostring(contacts)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.