Hey friends I am generating XML data using Python libraries as follow
def multiwan_info_save(request):
data = {}
init = "init"
try:
form = Addmultiwanform(request.POST)
except:
pass
if form.is_valid():
from_sv = form.save(commit=False)
obj_get = False
try:
obj_get = MultiWAN.objects.get(isp_name=from_sv.isp_name)
except:
obj_get = False
nameservr = request.POST.getlist('nameserver_mw')
for nm in nameservr:
nameserver1, is_new = NameServer.objects.get_or_create(name=nm)
from_sv.nameserver = nameserver1
from_sv.save()
# main(init)
top = Element('ispinfo')
# comment = Comment('Generated for PyMOTW')
#top.append(comment)
all_connection = MultiWAN.objects.all()
for conn in all_connection:
child = SubElement(top, 'connection number ='+str(conn.id)+'name='+conn.isp_name+'desc='+conn.description )
subchild_ip = SubElement(child,'ip_address')
subchild_subnt = SubElement(child,'subnet')
subchild_gtwy = SubElement(child,'gateway')
subchild_nm1 = SubElement(child,'probe_server1')
subchild_nm2 = SubElement(child,'probe_server2')
subchild_interface = SubElement(child,'interface')
subchild_weight = SubElement(child,'weight')
subchild_ip.text = str(conn.ip_address)
subchild_subnt.text = str(conn.subnet)
subchild_gtwy.text = str(conn.gateway)
subchild_nm1.text = str(conn.nameserver.name)
# subchild_nm2.text = conn.
subchild_weight.text = str(conn.weight)
subchild_interface.text = str(conn.interface)
print "trying to print _____________________________"
print tostring(top)
print "let seeeeeeeeeeeeeeeeee +++++++++++++++++++++++++"
But I am getting output like follow
<ispinfo><connection number =5name=Airtelllldesc=Largets TRelecome ><ip_address>192.168.1.23</ip_address><subnet>192.168.1.23</subnet><gateway>192.168.1.23</gateway><probe_server1>192.168.99.1</probe_server1><probe_server2 /><interface>eth0</interface><weight>160</weight></connection number =5name=Airtelllldesc=Largets TRelecome ><connection number =6name=Uninordesc=Uninor><ip_address>192.166.55.23</ip_address><subnet>192.166.55.23</subnet><gateway>192.168.1.23</gateway><probe_server1>192.168.99.1</probe_server1><probe_server2 /><interface>eth0</interface><weight>160</weight></connection number =6name=Uninordesc=Uninor><connection number =7name=Airteldesc=Largets TRelecome ><ip_address>192.168.1.23</ip_address><subnet>192.168.1.23</subnet><gateway>192.168.1.23</gateway><probe_server1>192.168.99.1</probe_server1><probe_server2 /><interface>eth0</interface><weight>160</weight></connection number =7name=Airteldesc=Largets TRelecome ></ispinfo>
I just want to know that how can I write this XML in proper XML format ?
Thanks in advance
UPDATED to include simulation of both creating and printing of the XML tree
The Basic Issue
Your code is generating invalid connection tags like this:
<connection number =5name=Airtelllldesc=Largets TRelecome ></connection number =5name=Airteldesc=Largets TRelecome >
when they should look like this (I am omitting the sub-elements in between. Your code is generating these correctly):
<connection number="5" name="Airtellll" desc="Largets TRelecome" ></connection>
If you had valid XML, this code would print it neatly:
from lxml import etree
xml = '''<ispinfo><connection number="5" name="Airtellll" desc="Largets TRelecome" ><ip_address>192.168.1.23</ip_address><subnet>192.168.1.23</subnet><gateway>192.168.1.23</gateway><probe_server1>192.168.99.1</probe_server1><probe_server2 /><interface>eth0</interface><weight>160</weight></connection></ispinfo>'''
xml = etree.XML(xml)
print etree.tostring(xml, pretty_print = True)
Generating Valid XML
A small simulation follows:
from lxml import etree
# Some dummy text
conn_id = 5
conn_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 question
root = 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 it
print etree.tostring(root, pretty_print=True)
This will produce:
<ispinfo>
<connection desc="Largets TRelecome" number="5" name="Airtelll">
<ip_address>192.168.1.23</ip_address>
</connection>
</ispinfo>
A single line is proper, in the sense that a XML parser will understand it.
For pretty-printing to sys.stdout, use the dump method of Element.
For pretty-printing to a stream, use the write method of ElementTree.
Related
I need to parse a soap response and convert to a text file. I am trying to parse the values as detailed below. I am using ElementTree in python
I have the below xml response which I need to parse
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tmf854="tmf854.v1" xmlns:alu="alu.v1">
<soapenv:Header>
<tmf854:header>
<tmf854:activityName>query</tmf854:activityName>
<tmf854:msgName>queryResponse</tmf854:msgName>
<tmf854:msgType>RESPONSE</tmf854:msgType>
<tmf854:senderURI>https:/destinationhost:8443/tmf854/services</tmf854:senderURI>
<tmf854:destinationURI>https://localhost:8443</tmf854:destinationURI>
<tmf854:activityStatus>SUCCESS</tmf854:activityStatus>
<tmf854:correlationId>1</tmf854:correlationId>
<tmf854:communicationPattern>MultipleBatchResponse</tmf854:communicationPattern>
<tmf854:communicationStyle>RPC</tmf854:communicationStyle>
<tmf854:requestedBatchSize>1500</tmf854:requestedBatchSize>
<tmf854:batchSequenceNumber>1</tmf854:batchSequenceNumber>
<tmf854:batchSequenceEndOfReply>true</tmf854:batchSequenceEndOfReply>
<tmf854:iteratorReferenceURI>http://9195985371165397084</tmf854:iteratorReferenceURI>
<tmf854:timestamp>20220915222121.472+0530</tmf854:timestamp>
</tmf854:header>
</soapenv:Header>
<soapenv:Body>
<queryResponse xmlns="alu.v1">
<queryObjectData>
<queryObject>
<name>
<tmf854:mdNm>AMS</tmf854:mdNm>
<tmf854:meNm>CHEERLAVANCHA_281743</tmf854:meNm>
<tmf854:ptpNm>/type=NE/CHEERLAVANCHA_281743</tmf854:ptpNm>
</name>
<vendorExtensions>
<package>
<NameAndStringValue>
<tmf854:name>hubSubtendedStatus</tmf854:name>
<tmf854:value>NONE</tmf854:value>
</NameAndStringValue>
<NameAndStringValue>
<tmf854:name>productAndRelease</tmf854:name>
<tmf854:value>DF.6.1</tmf854:value>
</NameAndStringValue>
<NameAndStringValue>
<tmf854:name>adminUserName</tmf854:name>
<tmf854:value>isadmin</tmf854:value>
</NameAndStringValue>
<NameAndStringValue>
</package>
</vendorExtensions>
</queryObject>
</queryObjectData>
</queryResponse>
</soapenv:Body>
</soapenv:Envelope>
I need to use the below code snippet.
parser = ElementTree.parse("response.txt")
root = parser.getroot()
inventoryObjectData = root.find(".//{alu.v1}queryObjectData")
for inventoryObject in inventoryObjectData:
for device in inventoryObject:
if (device.tag.split("}")[1]) == "me":
vendorExtensionsNames = []
vendorExtensionsValues = []
if device.find(".//{tmf854.v1}mdNm") is not None:
mdnm = device.find(".//{tmf854.v1}mdNm").text
if device.find(".//{tmf854.v1}meNm") is not None:
menm = device.find(".//{tmf854.v1}meNm").text
if device.find(".//{tmf854.v1}userLabel") is not None:
userlabel = device.find(".//{tmf854.v1}userLabel").text
if device.find(".//{tmf854.v1}resourceState") is not None:
resourcestate = device.find(".//{tmf854.v1}resourceState").text
if device.find(".//{tmf854.v1}location") is not None:
location = device.find(".//{tmf854.v1}location").text
if device.find(".//{tmf854.v1}manufacturer") is not None:
manufacturer = device.find(".//{tmf854.v1}manufacturer").text
if device.find(".//{tmf854.v1}productName") is not None:
productname = device.find(".//{tmf854.v1}productName").text
if device.find(".//{tmf854.v1}version") is not None:
version = device.find(".//{tmf854.v1}version").text
vendorExtensions = device.find("vendorExtensions")
vendorExtensionsNamesElements = vendorExtensions.findall(".//{tmf854.v1}name")
for i in vendorExtensionsNamesElements:
vendorExtensionsNames.append(i.text.strip())
vendorExtensionsValuesElements = vendorExtensions.findall(".//{tmf854.v1}value")
for i in vendorExtensionsValuesElements:
vendorExtensionsValues.append(str(i.text or "").strip())
alu = ""
for i in vendorExtensions:
if i.attrib:
if alu == "":
alu = i.attrib.get("{alu.v1}name")
else:
alu = alu + "|" + i.attrib.get("{alu.v1}name")
The issue is that The below code is not able to find the 'vendorExtensions"'. Please help here.
vendorExtensions = device.find("vendorExtensions")
Have tried the below as well
vendorExtensions = device.find(".//queryObject/vendorExtensions")
Your document declares a default namespace of alu.v1:
<queryResponse xmlns="alu.v1">
...
</queryResponse>
Any attribute without an explicit namespace is in the alu.v1 namespace. You need to qualify your attribute name appropriately:
vendorExtensions = device.find("{alu.v1}vendorExtensions")
While the above is a real problem with your code that needs to be corrected (the Wikipedia entry on XML namespaces may be useful reading if you're unfamiliar with how namespaces work), there are also some logic problems with your code.
Let's drop the big list of conditionals from the code and see if it's actually doing what we think it's doing. If we run this:
from xml.etree import ElementTree
parser = ElementTree.parse("data.xml")
root = parser.getroot()
queryObjectData = root.find(".//{alu.v1}queryObjectData")
for queryObject in queryObjectData:
for device in queryObject:
print(device.tag)
Then using your sample data (once it has been corrected to be syntactically valid), we see as output:
{alu.v1}name
{alu.v1}vendorExtensions
Your search for the {alu.v1}vendorExtensions element will never succeed before the thing on which you're trying to search (the device variable) is the thing you're trying to find.
Additionally, the conditional in your loop...
if (device.tag.split("}")[1]) == "me":
...will never match (there is no element in the entire document for which tag.split("}")[1] == "me" is True).
I'm not entirely clear what you're trying to do, but here's are some thoughts:
Given your example data, you probably don't want that for device in inventoryObject: loop
We can drastically simplify your code by replacing that long block of conditionals with a list of attributes in which we are interested and then a for loop to extract them.
Rather than assigning a bunch of individual variables, we can build up a dictionary with the data from the queryObject
That might look like:
from xml.etree import ElementTree
import json
attributeNames = [
"mdNm",
"meNm",
"userLabel",
"resourceState",
"location",
"manufacturer",
"productName",
"version",
]
parser = ElementTree.parse("data.xml")
root = parser.getroot()
queryObjectData = root.find(".//{alu.v1}queryObjectData")
for queryObject in queryObjectData:
device = {}
for name in attributeNames:
if (value := queryObject.find(f".//{{tmf854.v1}}{name}")) is not None:
device[name] = value.text
vendorExtensions = queryObject.find("{alu.v1}vendorExtensions")
extensionMap = {}
for extension in vendorExtensions.findall(".//{alu.v1}NameAndStringValue"):
extname = extension.find("{tmf854.v1}name").text
extvalue = extension.find("{tmf854.v1}value").text
extensionMap[extname] = extvalue
device["vendorExtensions"] = extensionMap
print(json.dumps(device, indent=2))
Given your example data, this outputs:
{
"mdNm": "AMS",
"meNm": "CHEERLAVANCHA_281743",
"vendorExtensions": {
"hubSubtendedStatus": "NONE",
"productAndRelease": "DF.6.1",
"adminUserName": "isadmin"
}
}
An alternate approach, in which we just transform each queryObject into a dictionary, might look like this:
from xml.etree import ElementTree
import json
def localName(ele):
return ele.tag.split("}")[1]
def etree_to_dict(t):
if list(t):
d = {}
for child in t:
if localName(child) == "NameAndStringValue":
d.update(dict([[x.text.strip() for x in child]]))
else:
d.update({localName(child): etree_to_dict(child) for child in t})
return d
else:
return t.text.strip()
parser = ElementTree.parse("data.xml")
root = parser.getroot()
queryObjectData = root.find(".//{alu.v1}queryObjectData") or []
for queryObject in queryObjectData:
d = etree_to_dict(queryObject)
print(json.dumps(d, indent=2))
This will output:
{
"name": {
"mdNm": "AMS",
"meNm": "CHEERLAVANCHA_281743",
"ptpNm": "/type=NE/CHEERLAVANCHA_281743"
},
"vendorExtensions": {
"package": {
"hubSubtendedStatus": "NONE",
"productAndRelease": "DF.6.1",
"adminUserName": "isadmin"
}
}
}
That may or may not be appropriate depending on the structure of your real data and exactly what you're trying to accomplish.
I am building the xml format.
I want to do something like this:
<ValidationRequest>
<Username>admin#xgateinternal.com</Username>
<Password>XBR!t6YMeJbmsjqV</Password>
<Email>12345#gmail.com</Email>
<Email>564685#yahoo.com.hk</Email>
<Email>54321#yahoo.com.hk</Email>
</ValidationRequest>
Now I can append all email into ValidationRequest. But I got the wrong format.
This is my code:
#!/usr/bin/python
import lxml.etree
import lxml.builder
email = ['12345#gmail.com','564685#yahoo.com.hk','54321#yahoo.com.hk']
E = lxml.builder.ElementMaker()
ValidationRequest = E.ValidationRequest
Username = E.Username
Password = E.Password
Email = E.Email
op = []
for a in email:
GG = Email(a)
op.append(lxml.etree.tostring(GG, pretty_print=True))
print (lxml.etree.tostring(GG, pretty_print=True))
print(op)
ed = ''.join(map(str, op))
print(ed)
the_doc = ValidationRequest(
Username('admin'),
Password('XBR'),
ed
)
print (lxml.etree.tostring(the_doc, pretty_print=True))
The response:
b'<Email>12345#gmail.com</Email>\n'
b'<Email>564685#yahoo.com.hk</Email>\n'
b'<Email>54321#yahoo.com.hk</Email>\n'
[b'<Email>12345#gmail.com</Email>\n', b'<Email>564685#yahoo.com.hk</Email>\n', b'<Email>54321#yahoo.com.hk</Email>\n']
b'<Email>12345#gmail.com</Email>\n'b'<Email>564685#yahoo.com.hk</Email>\n'b'<Email>54321#yahoo.com.hk</Email>\n'
b"<ValidationRequest><Username>admin</Username><Password>XBR</Password>b'<Email>12345#gmail.com</Email>\\n'b'<Email>564685#yahoo.com.hk</Email>\\n'b'<Email>54321#yahoo.com.hk</Email>\\n'</ValidationRequest>\n"
How can I keep <Email></Email> and post to ValidationRequest?
You had the right idea, there's just a lot there that's just moving stuff around. You can bring it down to this:
import lxml.etree
import lxml.builder
email = ['12345#gmail.com','564685#yahoo.com.hk','54321#yahoo.com.hk']
em = lxml.builder.ElementMaker()
the_doc = em.ValidationRequest(
em.Username('admin'),
em.Password('XBR'),
*(em.Email(address) for address in email)
)
print(lxml.etree.tostring(the_doc, pretty_print=True).decode())
The .decode() is only there for it to print nicely. If you need the bytes, you can just leave that off.
Note this line in particular:
*(em.Email(address) for address in email)
This loops over email, generating an Email element for each address as it goes, and then unpacking the resulting tuple (which is the result of the parentheses surrounding the generator) in to the constructor for the ValidationRequest element.
Output:
<ValidationRequest>
<Username>admin</Username>
<Password>XBR</Password>
<Email>12345#gmail.com</Email>
<Email>564685#yahoo.com.hk</Email>
<Email>54321#yahoo.com.hk</Email>
</ValidationRequest>
I have an xml file which looks like this:
<keyboard>
</keyboard>
I want to make it look like this:
<keyboard>
<keybind key="W-c-a"><action name="Execute"><command>sudo shutdown now</command></action></keybind>
</keyboard>
I have a function to add this which has parameters that will change the key and the command. Is this possible to do? If yes, how can I do this?
(The function):
def add_keybinding(self, keys, whatToExec):
keybinding = "<keybind key=\"%s\"><action name=\"Execute\"><command>%s</command><action></keybind>" % (keys, whatToExec)
f = open("/etc/xdg/openbox/rc.xml", "a")
try:
# I want to append the keybinding variable to the <keyboard>
except IOError as e:
print(e)
From the doc, you can try the following:
def add_keybinding(keys, whatToExec, filename):
keybind = ET.Element('keybind')
keybind.set("key", keys)
action = ET.SubElement(keybind, 'action')
action.set("name", "Execute")
command = ET.SubElement(action, 'command')
command.text = whatToExec
tree = ET.parse(filename)
tree.getroot().append(keybind)
tree.write(filename)
Explanation:
Create the keybind tag using xml.etree.ElementTree.Element : keybind = ET.Element('keybind')
Set a property using set: keybind.set("key", keys)
Create the action tag as a sub element of keybind using
xml.etree.ElementTree.SubElement: action = ET.SubElement(keybind, 'action')
Set the property as at step 2: action.set("name", "Execute")
Create command tag: action.set("name", "Execute")
Set command tag content using .text: command.text = whatToExec
Read file using xml.etree.ElementTree.parse: tree = ET.parse(filename)
Append keybind tag to the doc root element using append*
Export new xml to file using write
Full example:
import xml.etree.ElementTree as ET
from xml.dom import minidom
def add_keybinding(keys, whatToExec, filename):
keybind = ET.Element('keybind')
keybind.set("key", keys)
action = ET.SubElement(keybind, 'action')
action.set("name", "Execute")
command = ET.SubElement(action, 'command')
command.text = whatToExec
tree = ET.parse(filename)
tree.getroot().append(keybind)
tree.write(filename)
return tree
def prettify(elem):
rough_string = ET.tostring(elem, 'utf-8')
return minidom.parseString(rough_string).toprettyxml(indent=" ")
filename = "test.xml"
for i in range(3):
tree = add_keybinding(str(i), "whatToExec " + str(i), filename)
print(prettify(tree.getroot()))
Output:
<?xml version="1.0" ?>
<keyboard>
<keybind key="0">
<action name="Execute">
<command>whatToExec 0</command>
</action>
</keybind>
<keybind key="1">
<action name="Execute">
<command>whatToExec 1</command>
</action>
</keybind>
<keybind key="2">
<action name="Execute">
<command>whatToExec 2</command>
</action>
</keybind>
</keyboard>
I am a complete Python noob
Now that the foreshadowing is done, I am trying to parse some information out of a SOAP response.
The body of the reponse is below:
<soap:Body>
<ProcessMessageResponse xmlns="http://www.starstandards.org/webservices/2005/10/transport">
<payload>
<content id="Content0">
<CustomerLookupResponse xmlns="">
<Customer>
<CompanyNumber>ZQ1</CompanyNumber>
<CustomerNumber>1051012</CustomerNumber>
<TypeCode>I</TypeCode>
<LastName>NAME</LastName>
<FirstName>BASIC</FirstName>
<MiddleName/>
<Salutation/>
<Gender/>
<Language/>
<Address1/>
<Address2/>
<Address3/>
<City/>
<County/>
<StateCode/>
<ZipCode>0</ZipCode>
<PhoneNumber>0</PhoneNumber>
<BusinessPhone>0</BusinessPhone>
<BusinessExt>0</BusinessExt>
<FaxNumber>0</FaxNumber>
<BirthDate>0</BirthDate>
<DriversLicense/>
<Contact/>
<PreferredContact/>
<MailCode/>
<TaxExmptNumber/>
<AssignedSalesperson/>
<CustomerType/>
<PreferredPhone/>
<CellPhone>0</CellPhone>
<PagePhone>0</PagePhone>
<OtherPhone>0</OtherPhone>
<OtherPhoneDesc/>
<Email1/>
<Email2/>
<OptionalField/>
<AllowContactByPostal/>
<AllowContactByPhone/>
<AllowContactByEmail/>
<BusinessPhoneExtension/>
<InternationalBusinessPhone/>
<InternationalCellPhone/>
<ExternalCrossReferenceKey>0</ExternalCrossReferenceKey>
<InternationalFaxNumber/>
<InternationalOtherPhone/>
<InternationalHomePhone/>
<CustomerPreferredName/>
<InternationalPagerPhone/>
<PreferredLanguage/>
<LastChangeDate>20130401</LastChangeDate>
<Vehicles/>
<CCID/>
<CCCD>0</CCCD>
</Customer>
</CustomerLookupResponse>
</content>
</payload>
</ProcessMessageResponse>
</soap:Body>
and I have the following code snippet to show what I have done to parse out the response I want:
customer_number = ''
customer_first_name = ''
customer_last_name = ''
def send_customer_lookup(data):
soap_action = 'http://www.starstandards.org/webservices/2005/10/transport/operations/ProcessMessage'
source_port = random.randint(6000, 20000)
webservice = httplib.HTTPSConnection('otqa.arkona.com', source_address=('', source_port))
webservice.putrequest('POST', '/OpenTrack/Webservice.asmx?wsdl')
webservice.putheader('User-Agent', 'OpenTrack-Heartbeat')
webservice.putheader('Content-Type', 'application/soap+xml')
webservice.putheader('Content-Length', '%d' % len(data))
webservice.putheader('SOAPAction', soap_action)
webservice.endheaders()
webservice.send(data)
response = webservice.getresponse()
response_xml = str(response.read())
doc = ET.fromstring(response_xml)
for customer in doc.findall('.//{http://www.starstandards.org/webservices/2005/10/transport}Payload'):
global customer_number
global customer_first_name
global customer_last_name
customer_number = customer.findtext('{http://www.starstandards.org/webservices/2005/10/transport}CustomerNumber')
customer_first_name = customer.findtext('{http://www.starstandards.org/webservices/2005/10/transport}FirstName')
customer_last_name = customer.findtext('{http://www.starstandards.org/webservices/2005/10/transport}LastName')
webservice.close()
return customer_number, customer_first_name, customer_last_name, response_xml
I am not certain why I am getting an output of ' ', ' ', ' ', <xml response>...
It looks like you are overspecifying the field names, therefore they don't match anything, therefore your for customer in ... never runs. Try this:
import httplib
import xml.etree.ElementTree as ET
def send_customer_lookup(data):
soap_action = 'http://www.starstandards.org/webservices/2005/10/transport/operations/ProcessMessage'
source_port = random.randint(6000, 20000)
with httplib.HTTPSConnection('otqa.arkona.com', source_address=('', source_port)) as webservice:
webservice.putrequest('POST', '/OpenTrack/Webservice.asmx?wsdl')
webservice.putheader('User-Agent', 'OpenTrack-Heartbeat')
webservice.putheader('Content-Type', 'application/soap+xml')
webservice.putheader('Content-Length', '%d' % len(data))
webservice.putheader('SOAPAction', soap_action)
webservice.endheaders()
webservice.send(data)
response_xml = str(webservice.getresponse().read())
doc = ET.fromstring(response_xml)
results = []
for customer in doc.findall('.//CustomerLookupResponse/'):
customer_number = customer.findtext('CustomerNumber')
customer_first_name = customer.findtext('FirstName')
customer_last_name = customer.findtext('LastName')
results.append((customer_number, customer_first_name, customer_last_name))
return results
Also, global variable names are generally evil; I presume that you added them because you were getting 'variable not defined' errors? That should have been a clue that the for-loop was not actually getting run.
you could use xml.dom.minidom :
from xml.dom import minidom
def parse_customer_data(response_xml):
results = []
dom = minidom.parseString(response_xml)
customers=dom.getElementsByTagName('Customer')
for c in customers:
results.append({
"cnum" : c.getElementsByTagName('CustomerNumber')[0].firstChild.data,
"lname" : c.getElementsByTagName('LastName')[0].firstChild.data,
"fname" : c.getElementsByTagName('FirstName')[0].firstChild.data
})
return results
if __name__ == "__main__":
response_xml = open("soap.xml").read()
results = parse_customer_data(response_xml)
print(results)
note that for the input file, soap.xml:
1. I added xml version / soap:Envelope elements around the XML you provided, otherwise it would not parse
2. I added another Customer element to test my code
output:
$ python soap.py
[{'lname': u'NAME1', 'cnum': u'1051012', 'fname': u'BASIC1'}, {'lname': u'NAME2', 'cnum': u'1051013', 'fname': u'BASIC2'}]
Kindly have a look at below code i am using this to generate a xml using python .
from lxml import etree
# Some dummy text
conn_id = 5
conn_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 question
root = 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 it
print etree.tostring(root, pretty_print=True)
This will produce:
<ispinfo>
<connection desc="Largets TRelecome" number="5" name="Airtelll">
<ip_address>192.168.1.23</ip_address>
</connection>
</ispinfo>
But i want it to be like :
<ispinfo>
<connection desc="Largets TRelecome" number='1' name="Airtelll">
<ip_address>192.168.1.23</ip_address>
</connection>
</ispinfo>
Mean number attribute should be come in a single quote .Any idea ....How can i achieve this
There is no flag in lxml to do this, so you have to resort to manual manipulation.
import re
re.sub(r'number="([0-9]+)"',r"number='\1'", etree.tostring(root, pretty_print=True))
However, why do you want to do this? As there is no difference other than cosmetics.