0

I am reading a data from table and want to converting to XML file.

I am successfully read data from table and store in dataframe but not able to convert to xml.

Reading the data from table to dataframe

       import cx_Oracle
            import pandas as pd
            from datetime import date
            import time

             CONN_INFO = {
                    'host': 'xxxxxx',
                    'port': 1521,
                    'user': 'xxxxx',
                    'psw': 'xxxxx',
                    'service': 'xxxxx',
                }

                CONN_STR = '{user}/{psw}@{host}:{port}/{service}'.format(**CONN_INFO)

                connection=cx_Oracle.connect(CONN_STR)
                cursor=connection.cursor()

    cursor.execute("""with M0 as (select 
         os.order_id order_id
         , MIN(os.EFFECTIVE_TIMESTAMP) as M0_DT
    FROM
        V_F_OR_STATUS_HISTORY    os 
    WHERE
         os.status_type IN ('0170','0112','2364','2363')
         AND trunc(os.EFFECTIVE_TIMESTAMP)  >= trunc(sysdate)-5
    GROUP BY
         os.order_id 
    """)
 data=cursor.fetchall()

Output

 [('GLAXO.4501201394', datetime.datetime(2019, 8, 29, 0, 0)),
 ('GLAXO.SG70X4501162986-THNEO', datetime.datetime(2019, 9, 26, 0, 0)),
 ('GLAXO.8000334269', datetime.datetime(2019, 9, 5, 0, 0)),
 ('GLAXO.20190828168057', datetime.datetime(2019, 11, 6, 0, 0)),
 ('GLAXO.PH22X4501136042-THNEO', datetime.datetime(2019, 10, 13, 0, 0)),
 ('GLAXO.4501205247', datetime.datetime(2019, 11, 3, 0, 0)),
 ('GLAXO.256286313', datetime.datetime(2019, 10, 21, 0, 0)),
 ('GLAXO.4501205964', datetime.datetime(2019, 9, 7, 0, 0))]

Coverting to XML

I tried the below refernce avialable from stackoverlow its not converting to xml

 root = etree.Element('data');

    for i,row in data.iterrows():
        item = etree.SubElement(root, 'item', attrib=row.to_dict());

  etree.dump(root);

    xml.etree.ElementTree.parse('xml_file.xml');

How to fix this. Any help is appreciated

Rahul rajan
  • 1,186
  • 4
  • 18
  • 32

0 Answers0