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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:05:16+00:00 2026-06-13T05:05:16+00:00

Possible Duplicate: Creating a simple XML file using python I want to write a

  • 0

Possible Duplicate:
Creating a simple XML file using python

I want to write a XML file from Python.
The XML is like the following format only:

<Title rollid="1" mainid="1" teamid="1">
<s name="hello" address"abcdef" "etc"/>
 <s name="" address="" />
</Title>

I wrote code in Python using lxml and etree but the XML file which I get is like this:

<Title>
<s>rollid=""1" mainid="1"</s>
<s>name="" address=""</s>
<s>name="" address=""</s>
</Title>

Please let me know how to get the desired format

My Code:

import os
import sys

import lxml.builder as lb
from lxml import etree

#i made a dummy file AddDetail.xml with the root tags

def WriteDetails(rolid,mainid,name,address):
    myhash=dict()   # Declaring a dictionary

    #Storing the data which has to be written to xml in a dictionary
    myhash={'rollid':rolid, 'mainid':mainid,  'name':name, 'opid':opid, 'address':address}

    # Converting the data from dictionary to string for XML and 
    also checking if any valueis 0
    data=' '.join([('%s="%s"')%(key,value) for key,value in myhash.iteritems()if value])

    # Creating the root Element
    root=etree.Element("Title")

    # Making a new Document Tree
    doc=etree.parse('AddDetail.xml')

    # Getting the root tag
    root=doc.getroot()

    # Adding a new Element
     y=lab.E.Title(lb.E.s(data),
    rollid="1" mainid="1" teamid="1")
    print etree.tostring(y,pretty_print=true)

   output i get is

   <Title rollid="1" mainid="1" teamid="1">
   <s>name="hello" address="aaaa"</s>
   </Title>

   I need something like
   <Title rollid="1" mainid="1" teamid="1">
   <s name="hello" address="aaaa"/>
   </Title>
  • 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-13T05:05:17+00:00Added an answer on June 13, 2026 at 5:05 am

    Use lxml.builder, here’s a tutorial too.

       import lxml.builder as lb
       from lxml import etree
    
    y=lb.E.Title(lb.E.s(name="hello",adress="abcdef"),
                 lb.E.s(name="",adress=""),
                 rollid="1", mainid="1",teamid="1")
    
    print etree.tostring(y, pretty_print=True)
    
    >>> 
    <Title teamid="1" rollid="1" mainid="1">
      <s adress="abcdef" name="hello"/>
      <s adress="" name=""/>
    </Title>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: A simple program to CRUD node and node values of xml file
Possible Duplicate: Creating or assigning variables from a dictionary in Python Hello we have
Possible Duplicate: Bottle-friendly WSGI authentication library/middleware I'm creating a simple web application with Python
Possible Duplicate: Selecting Random Rows in MySQL I'm creating a simple web application using
Possible Duplicate: Creating an abstract class in Objective C In Java I like to
Possible Duplicate: XNA Mouse Movement Basically, I am creating a game using XNA, and
Possible Duplicate: What is the 'new' keyword in JavaScript? creating objects from JS closure:
Possible Duplicate: Is there a clean way to prevent windows.h from creating a near
Possible Duplicate: Creating Nondeterministic functions in SQL Server using RAND() I need to generate
Possible Duplicate: Mysql: Setup the format of DATETIME to 'DD-MM-YYYY HH:MM:SS' when creating a

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.