-1

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>
3
  • 7
    show us your code Commented Oct 17, 2012 at 7:17
  • stackoverflow.com/questions/3605680/… Commented Oct 17, 2012 at 7:23
  • this looks like a typo: address"abcdef" Commented Oct 17, 2012 at 7:28

2 Answers 2

1

You need to learn how to create attributes:

http://lxml.de/tutorial.html#elements-carry-attributes

>>> root = etree.Element("root", interesting="totally")
>>> etree.tostring(root)
b'<root interesting="totally"/>'
Sign up to request clarification or add additional context in comments.

Comments

0

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>

3 Comments

if instead of printing the value of name and address, if i pass a variable then the format gets changed. i get something like <s>dasff</s>
editied my old question only..
no, you should not edit your old question, if you have a new one. meta.stackexchange.com/questions/145773/… your original question has been correctly answered.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.