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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:50:13+00:00 2026-05-31T15:50:13+00:00

Here is the code: from xml.dom.minidom import Document doc = Document() root = doc.createElement(‘root’)

  • 0

Here is the code:

    from xml.dom.minidom import Document

doc = Document()
root = doc.createElement('root')
doc.appendChild(root)
for i in range(1,3):
    main = doc.createElement('item class:=memory')
    root.appendChild(main)
    for j in range(1,3):
        text = doc.createTextNode('DIMM Size'+str(j))
        main.appendChild(text)

print (doc.toprettyxml(indent='\t'))

Here is the output:

     <?xml version="1.0" ?>
<root>
    <item class:=memory>
        DIMM Size1
        DIMM Size2
    </item class:=memory>
    <item class:=memory>
        DIMM Size1
        DIMM Size2
    </item class:=memory>
</root>

I am trying to generate the file with following code. Is there a way to generate the following output:

<root>
    <item class:=memory>
        <p> DIMM Size1 </p>
        <p>DIMM Size2 </p>
    </item>
    <item class:=memory>
        <p>DIMM Size1</p>
        <p>DIMM Size2</p>
    </item>
</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-31T15:50:14+00:00Added an answer on May 31, 2026 at 3:50 pm

    You need two quick changes

    1. Create a p element e.g. doc.createElement('p')
    2. Don’t set attributes manually instead use node.attributes e.g. main.attributes['class']='memory'

    so your code should look like this

    from xml.dom.minidom import Document
    
    doc = Document()
    root = doc.createElement('root')
    doc.appendChild(root)
    for i in range(1,3):
        main = doc.createElement('item')
        main.attributes['class']='memory'
        root.appendChild(main)
        for j in range(1,3):
            p = doc.createElement('p')
            text = doc.createTextNode('DIMM Size'+str(j))
            p.appendChild(text)
            main.appendChild(p)
    
    print (doc.toprettyxml(indent='\t'))
    

    A long term change would be to use ElementTree which has more intuitive interface and is easy to use, more so while reading xml e.g. your example in element tree

    from xml.etree import cElementTree as etree
    
    root = etree.Element('root')
    for i in range(1,3):
        item = etree.SubElement(root, 'item')
        item.attrib['class']='memory'
        for j in range(1,3):
            p = etree.SubElement(item, 'p')
            p.text = 'DIMM Size %s'%j
    
    print etree.tostring(root)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my sample code: from xml.dom.minidom import * def make_xml(): doc = Document()
I have the following code. from xml.dom.minidom import Document doc = Document() root =
Here's the code I have: from cStringIO import StringIO from lxml import etree xml
Here is the example code from my web.xml <security-constraint> <display-name> change password</display-name> <web-resource-collection> <web-resource-name>change
Here is code from MSDN . I don't understand why the work isn't just
Here's the code from the ascx that has the repeater: <asp:Repeater ID=ListOfEmails runat=server >
I have got the following code from here to read an Excel file using
I'm using some code from here to determine when determining when the last finger
I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still
Here's a snippet of code from within TurboGears 1.0.6: [dispatch.generic(MultiorderGenericFunction)] def run_with_transaction(func, *args, **kw):

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.