Generating Xml using python

sleyer

New Member
Kindly have a look at below code i am using this to generate a xml using python .\[code\]from lxml import etree# Some dummy textconn_id = 5conn_name = "Airtelll"conn_desc = "Largets TRelecome"ip = "192.168.1.23"# Building the XML tree# Note how attributes and text are added, using the Element methods# and not by concatenating strings as in your questionroot = etree.Element("ispinfo")child = etree.SubElement(root, 'connection', number = str(conn_id), name = conn_name, desc = conn_desc)subchild_ip = etree.SubElement(child, 'ip_address')subchild_ip.text = ip# and pretty-printing itprint etree.tostring(root, pretty_print=True)\[/code\]This will produce:\[code\]<ispinfo> <connection desc="Largets TRelecome" number="5" name="Airtelll"> <ip_address>192.168.1.23</ip_address> </connection></ispinfo>\[/code\]But i want it to be like :\[code\]<ispinfo> <connection desc="Largets TRelecome" number='1' name="Airtelll"> <ip_address>192.168.1.23</ip_address> </connection></ispinfo>\[/code\]Mean number attribute should be come in a single quote .Any idea ....How can i achieve this
 
Back
Top