This example describes how Python uses MySQLdb to implement exporting an XML file from a database. Share to everyone for your reference. The specific analysis is as follows:
Here you need to provide some data to the front end in XML format, which already exists in the current database.
If you use Django to return XML data, you need to wrap the head information:
Copy the Code code as follows:
R = HttpResponse (str_xml)
R.mimetype = "Text/xml"
r[' content-type '] = "Application/xml"
In addition, using group by can be queried using the following methods.
Copy CodeThe code is as follows:
OBJS = Fish.objects.raw ("Select ID, almanac_name, style, almanac_code,almanac_description from Ppy_fish WHERE almanac_na Me! = ' GROUP by Almanac_code ')
For a simple example:
#-*-Coding:utf-8-*-from xml.dom import minidomimport mysqldbconn = mysqldb.connect (host= ' localhost ', user= ' root ', passwd= ' xxx ', db= ' my_xml ', charset= "UTF8") cursor = Conn.cursor () cursor.execute (' Select ID, name, style, description, Family from ppy_fish ') res_list = Cursor.fetchall () print len (res_list) doc = Minidom. Document () root = doc.createelement ("Data") doc.appendchild (root) ATTRIBUTE = {"n": 1, "D": 3}for res in res_list: node = Doc.createelement (res[2]) for i in ATTRIBUTE: Id_node = doc.createelement ("%s"% i) data = Doc.createtextnode ("%s"% res[attribute[i]) id_node.appendchild (data) Node.appendchild (Id_node) Root.appendchild (node) str_xml = Doc.toxml ("Utf-8") F = open (' Fish.xml ', ' W ') F.write (str_xml) f.close () Cursor.close () Conn.close ()
Hopefully this article will help you with Python programming.