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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:47:30+00:00 2026-06-11T12:47:30+00:00

How can I add an attribut and value to an XML document using xml.dom.minidom

  • 0

How can I add an attribut and value to an XML document using xml.dom.minidom in Python.

My XML is as follows

<?xml version="1.0" encoding="utf-8"?>

<PackageInfo xmlns="http://someurlpackage">


<data ID="http://someurldata1">data1</data >
<data ID="http://someurldata2">data2</data >
<data ID="http://someurldata3">data3</data >
</PackageInfo>

I want to add a new ‘data’ tag and it’s id as ‘http://someurldata4‘ and value as data4. So that the resulting xml will be as below. Sorry I don’t want to use xml.etree.ElementTree

<?xml version="1.0" encoding="utf-8"?>

<PackageInfo xmlns="http://someurlpackage">
<data ID="http://someurldata1">data1</data >
<data ID="http://someurldata2">data2</data >
<data ID="http://someurldata3">data3</data >
<data ID="http://someurldata4">data4</data >
</PackageInfo>
  • 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-11T12:47:31+00:00Added an answer on June 11, 2026 at 12:47 pm

    You create new DOM elements with the Document.createElement() method, new DOM attributes can be added with the Element.setAttribute() method:

    newdata = doc.createElement(u'data')
    newdata.setAttribute(u'ID', u'http://someurldata4')
    

    You then have to create a text node and add that as a child to the newdata Element, using the Document.createTextNode() and Node.appendChild() methods:

    newdata.appendChild(doc.createTextNode(u'data4'))
    

    Now you can add the new element to your document root:

    doc.documentElement.appendChild(newdata)
    

    In other words, use the Python implementation of the DOM API.

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

Sidebar

Related Questions

I have an xml document like this: <?xml version=1.0 encoding=UTF-8?> <foo:root xmlns:foo=http://abc.com# xmlns:bar=http://def.com xmlns:ex=http://ex.com>
This is my Xml file: <?xml version=1.0 encoding=utf-8 ?> <dati> <product id=456> <item>a</item> <item>b</item>
I try to generate such XML using REXML <root> <add key='foo' value='bar'/> </root> But
We can add an image to an photo album using UIImage *img = [UIImage
I'm transforming an XML document with PHP/XSL. I'm searching for a keyword value. I
I'm using C# to handle an XML file with a repeating structure. The pseudo-document
Possible Duplicate: Add an attribute with static value with xslt I have an XML
I am using the following code to modify the XML file that can be
can I add BehaviorID attribute for asp.net textbox and use it to be recognize
I can use TypeDescriptor.AddAttributes to add an attribute to a type in runtime. How

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.