473,394 Members | 1,703 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,394 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 2274
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
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.