473,287 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Create XML using python

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
<EMPLOYEE>
<EMPNO>123</EMPNO>
<ENAME>Shan</ENAME>
<SAL>2000</SAL>
<DEPTNO>100</DEPTNO>
</EMPLOYEE>
<EMPLOYEE>
<EMPNO>345</EMPNO>
<ENAME>Purna</ENAME>
<SAL>2000</SAL>
<DEPTNO>200</DEPTNO>
</EMPLOYEE>
May 30 '16 #1
3 2269
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
dwblas
626 Expert 512MB
You have not posted any code to analyze.
May 31 '16 #3
Hi,

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
  5.  
  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="  ")
  12.  
  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

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

Similar topics

0
by: Jon Moldover | last post by:
Hi, I'm using Python in my win32 app by linking to the python23.dll. I'm trying to expose some c++ code in my app to Python so I can make application calls from Python scripts (according to the...
3
by: Kay Lee | last post by:
Hi, I looked up os module to find out some method to move and copy files in python, but os doesn't support such methods. Is there any way to move & copy files in python? Thanks in adv.
10
by: Frog | last post by:
Hi, i'm not a programmer so I have a very stupid question. I'm trying to make a practical script. I need to run an executable program in it but i can't get it to work. Maybe someone here can...
8
by: Sridhar R | last post by:
Hi, I am a little experienced python programmer (2 months). I am somewhat experienced in C/C++. I am planning (now in design stage) to write an IDE in python. The IDE will not be a simple...
0
by: David Mitchell | last post by:
Hello group, I'm trying to create a TCP server using Python, and I want it to run under Windows as a service. Now, I'm fine with building the TCP server using Python - done it lots of times,...
0
by: Michael B. Trausch | last post by:
Hello, everyone. I am doing some searching and winding up a little bit confused. I have a MUD client that I am writing using Python and wxWidgets, as some of you may remember. What I am...
9
by: dominiquevalentine | last post by:
Hello, I'm a teen trying to do my part in improving the world, and me and my pal came up with some concepts to improve the transportation system. I have googled up and down for examples of using...
6
by: ronparker | last post by:
Hello, This is my first time posting and just my second day using python on mysql, so please be patient with me. I should also say, I am using a linux machine. Using python I was able to make a...
1
by: prii | last post by:
Hii guys I'm new to python and I'm trying to import mssql data to mysql using python.. #!/usr/bin/python import MySQLdb import pyodbc
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.