473,395 Members | 1,846 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,395 software developers and data experts.

cx_Oracle and python to get query results as XML?

I am using cx_Oracle and python 2.6 to get query results from oracle 10g database. Is there way using cx_Oracle or some other program to easily get resuls as XML. I presently take the python data structure and convert it json for use with javascripts. I now need to move to XML.
Any ideas or suggestions?
Sep 8 '10 #1
2 2643
bvdet
2,851 Expert Mod 2GB
The method of building your XML document would depend on your data structure and whether you want to save your data as text nodes, attributes, or a combination of the two. Following are examples that create XML documents from a simple dictionary.
Expand|Select|Wrap|Line Numbers
  1. '''
  2. Dictionary to XML
  3. The dictionary keys are XML tags and the values are text node values.
  4. '''
  5.  
  6. from xml.dom import minidom
  7.  
  8. def createTextNodes(dd):
  9.     xmldoc = minidom.Document()
  10.     xmldoc.appendChild(xmldoc.createComment("This is a comment"))
  11.  
  12.     root = xmldoc.createElement('root')
  13.     xmldoc.appendChild(root)
  14.  
  15.     for key in dd:
  16.         a = xmldoc.createElement(key)
  17.         b = xmldoc.createTextNode(str(dd[key]))
  18.         root.appendChild(a)
  19.         a.appendChild(b)
  20.  
  21.     return xmldoc
  22.  
  23. def createAttrs(dd):
  24.     xmldoc = minidom.Document()
  25.     xmldoc.appendChild(xmldoc.createComment("This is a comment"))
  26.  
  27.     root = xmldoc.createElement('root')
  28.     xmldoc.appendChild(root)
  29.  
  30.     dataNode = xmldoc.createElement('data')
  31.     root.appendChild(dataNode)
  32.  
  33.     for key in dd:
  34.         dataNode.setAttribute(key, str(dd[key]))
  35.  
  36.     return xmldoc
  37.  
  38. dd = {'code_version': 4.02, 'version': 1}
  39.  
  40. x1 = createTextNodes(dd)
  41. print x1.toprettyxml()
  42.  
  43. x2 = createAttrs(dd)
  44. print x2.toprettyxml()
The results:
Expand|Select|Wrap|Line Numbers
  1. >>> <?xml version="1.0" ?>
  2. <!--This is a comment-->
  3. <root>
  4.     <code_version>
  5.         4.02
  6.     </code_version>
  7.     <version>
  8.         1
  9.     </version>
  10. </root>
  11.  
  12. <?xml version="1.0" ?>
  13. <!--This is a comment-->
  14. <root>
  15.     <data code_version="4.02" version="1"/>
  16. </root>
  17.  
  18. >>> 
Sep 8 '10 #2
Sweet! I will give this a go and let you how it turns out. I like how you incorporated the <root> and comment. I will use that to incorporate a time stamp to my xml output.
Sep 8 '10 #3

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

Similar topics

1
by: Guy Erez | last post by:
Hi, I'm running queries with MySql 4.0.17 that return thousands of records. Because I need to present them in GUI, I returieve the results in chunks using LIMIT, for example - get first 100,...
6
by: Brian | last post by:
Hello, Basically, I'm running a query on a form's activation, and I'd like to have the results of the query be placed into other fields on the same form automatically. Does anybody know how...
0
by: Rob | last post by:
I doubt this is the best way to do it, but what I came up with was to hide the XML in an HTML Comment then edit the file deleting the HTML stuff and keep the XML results. If anyone has a better...
1
by: ljungers | last post by:
Hi and I hope that someone may have an answer for this, or an example of what I need to do. I have a Access database that a clerk will be entering a Order Number or Client Name or a Client City in a...
7
by: fcolon75 | last post by:
I'm an experienced Access user, but very new to coding VBA in Access. I'd like to do the following: 1) Develop a basic query in the query designer. 2) Call that query from a VBA script 3)...
9
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped....
2
by: Himmel | last post by:
Hello! The reference database I currently use runs queries that pull data from hundreds of tables in order to create user-friendly form view. The problem is that these queries can take upwards of...
1
by: aaronkm | last post by:
Hello thescripts and well met. I've recently been handed a new duty and have the joy of 'crash coursing' MS Access. Things are working well but I've ran into a problem that I can't seem to find...
1
by: igor221189 | last post by:
Hello everyone. I have Access 2000 database which holds student records in the school.It stores subject grades for each student.In the 'Student Grade Form', I would like to search student surname...
1
by: Arli | last post by:
I have the following linked tables: tblMainPL is my main table that I need to pull the information in from. It has the following fields: Autonumber1 -PK set as autonumber Date - short date...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.