Create XML using python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shanavas
    New Member
    • May 2016
    • 3

    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,SA L,DEPTNO"(colum n 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>
  • shanavas
    New Member
    • May 2016
    • 3

    #2
    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

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      You have not posted any code to analyze.

      Comment

      • shanavas
        New Member
        • May 2016
        • 3

        #4
        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
        Code:
        <employeedetails>
           <employee>
        	<empname>Smith</empname>
        	<empno>5102</empno>
        	<sal>600</sal>
        	<deptno>10</deptno>
           </employee>
           <employee>
        	<empname>john</empname>
        	<empno>4102</empno>
        	<sal>800</sal>
        	<deptno>20</deptno>
           </employee>
        <employeedetails>
        below is the code am using to generate the XML

        Code:
        from xml.etree.ElementTree import Element, SubElement, Comment
        from xml.etree import ElementTree
        from xml.dom import minidom
        import cx_Oracle
        
        def prettify(elem):
            """Return a pretty-printed XML string for the Element.
            """
            rough_string = ElementTree.tostring(elem, 'utf-8')
            reparsed = minidom.parseString(rough_string)
            return reparsed.toprettyxml(indent="  ")
        
        db = cx_Oracle.connect("username/pwd@servername") #Make it as a file read
        cursor = db.cursor()
        cursor.arraysize = 500
        cursor.execute("select * from employee") #Get the table name from input parameter
        top = Element('employeedetails')
        child = SubElement(top,'employee')
        for i in cursor:
            sub_child = SubElement(child, i[1])
            sub_child.text = i[2]
        print (prettify(top))
        insted of getting the desired output am getting ouput like below
        Code:
        <employeedetails>
           <employee>
        	<empname>Smith</empname>
        	<empno>5102</empno>
        	<sal>600</sal>
        	<deptno>10</deptno>
        	<empname>john</empname>
        	<empno>4102</empno>
        	<sal>800</sal>
        	<deptno>20</deptno>
           </employee>
        </employeedetails>
        Last edited by bvdet; Jun 15 '16, 03:24 PM. Reason: Add code tags

        Comment

        Working...