By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 1,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

Create XML using python

P: 3
Hi All,

I have been searching for a sample/basis python script which would create/generate a xml file out of oracle database. I don't have previous experience with Python. could you please help me on this. Below is the detail description on the task.

For example if i have table called "EMPLOYEE" in my oracle datbase and it is contains are "EMPNO,ENAME,SAL,DEPTNO"(column name) my XML file should looks like the below
May 30 '16 #1
Share this Question
Share on Google+
3 Replies

P: 3
hi All,

do you have any updates for me? If the requirement is not clear, do let me know to make a clear post over here. thanks
May 31 '16 #2

Expert 100+
P: 619
You have not posted any code to analyze.
May 31 '16 #3

P: 3

Thanks for your quick response and apologies for not coming back to you early. Recruitment had changed a bit and below are the full details.

I have a oracle table with the following data:

Tag_name, tag_value
empname Smith
empno 5102
sal 600
deptno 10
empname Jhon
empno 4102
sal 800
deptno 20

and my xml should look like
Expand|Select|Wrap|Line Numbers
  1. <employeedetails>
  2.    <employee>
  3.     <empname>Smith</empname>
  4.     <empno>5102</empno>
  5.     <sal>600</sal>
  6.     <deptno>10</deptno>
  7.    </employee>
  8.    <employee>
  9.     <empname>john</empname>
  10.     <empno>4102</empno>
  11.     <sal>800</sal>
  12.     <deptno>20</deptno>
  13.    </employee>
  14. <employeedetails>
below is the code am using to generate the XML

Expand|Select|Wrap|Line Numbers
  1. from xml.etree.ElementTree import Element, SubElement, Comment
  2. from xml.etree import ElementTree
  3. from xml.dom import minidom
  4. import cx_Oracle
  6. def prettify(elem):
  7.     """Return a pretty-printed XML string for the Element.
  8.     """
  9.     rough_string = ElementTree.tostring(elem, 'utf-8')
  10.     reparsed = minidom.parseString(rough_string)
  11.     return reparsed.toprettyxml(indent="  ")
  13. db = cx_Oracle.connect("username/pwd@servername") #Make it as a file read
  14. cursor = db.cursor()
  15. cursor.arraysize = 500
  16. cursor.execute("select * from employee") #Get the table name from input parameter
  17. top = Element('employeedetails')
  18. child = SubElement(top,'employee')
  19. for i in cursor:
  20.     sub_child = SubElement(child, i[1])
  21.     sub_child.text = i[2]
  22. print (prettify(top))
insted of getting the desired output am getting ouput like below
Expand|Select|Wrap|Line Numbers
  1. <employeedetails>
  2.    <employee>
  3.     <empname>Smith</empname>
  4.     <empno>5102</empno>
  5.     <sal>600</sal>
  6.     <deptno>10</deptno>
  7.     <empname>john</empname>
  8.     <empno>4102</empno>
  9.     <sal>800</sal>
  10.     <deptno>20</deptno>
  11.    </employee>
  12. </employeedetails>
Jun 6 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.