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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:54:15+00:00 2026-06-10T04:54:15+00:00

I would like to add to the end of an XML file using python.

  • 0

I would like to add to the end of an XML file using python.

The file is structured something like this:

<?xml version="1.0" ?>
<dic>
  <word-data>
    ...
  </word-data>
</dic>

And I would like to add another element in so that the file will look something like this:

<?xml version="1.0" ?>
<dic>
  <word-data>
    ...
  </word-data>
  <word-data>
    ...
  </word-data>
</dic>

Now for the programming Part!

What I am currently doing is “encoding” a list into xml using this function:

def make_xml(List):
    doc = Document();

    main = doc.createElement('dic')
    doc.appendChild(main)
    for l in List:
        parent = doc.createElement('word-data')
        main.appendChild(parent)

        for i in l:
            node = doc.createElement(i[0])
            node.appendChild(doc.createTextNode(str(i[1])))
            parent.appendChild(node)
    return doc

One of the lists looks like this:

List = [[['parent', 'node'],['parent', 'node'],['parent', 'node']]]

Now my question is how can I add this to the doc node in an existing XML file. I thought about turning the file into a list Then turning the doc into a new list, but I did not know how to do that, and I thought it may have been inefficient.

Anyways, any help is appreciated

  • 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-10T04:54:16+00:00Added an answer on June 10, 2026 at 4:54 am

    If what you want to do is append to the xml file use one of Python’s xml modules for instance for built-in xml.minidom http://docs.python.org/library/xml.dom.minidom.html

    I am a little confused about the various loops in your sample code and the nested list structure but based on the output sample you procided I think what you want is something like:

    from xml.dom.minidom import parse
    def make_xml(filename, elements):
        dom = parse(filename)
        top_element = dom.documentElement
    
        for elmnt in elements:
            parent = doc.createElement('word-data')
            top_element.appendChild(parent)
            parent.appendChild(doc.createTextNode(elmnt))
        return dom
    
    
    
    new_dom = add_to_xml_file('old_file.xml', ['foo','bar','baz'])
    
    # save file
    new_dom.writexml(open('new-bigger-file.xml','w'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to add a hash into an array using Ruby version 1.8.7:
I would like to add a Javascript at the end of some ajax content,
I have own project and i would like add for this class same as
I'm using xmlwriter to create an xml document. The xml document looks like this:
First of all, I got this huge xml file that represents data collected by
I have a ListView and each item have a TextView. I would like add
I would like to add a GestureDetector to all views (view groups) of an
I would like to add a custom calculation method to a managed object (which
I would like to add roots to a VirtualTreeView http://www.delphi-gems.com/index.php/controls/virtual-treeview with a thread like
I would like to add up the number of line changes in an SVN

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.