vendredi 27 février 2015

Creating complicated XML files with ElementTree

I am using SQLite to access a database and retrieve information. This information needs to be output in the form of an xml file. Using ElementTree (Must be compatible with Python 2.6!), I would like to know if there is an easy way to use the information I'm extracting to create a more complicated xml file similar to the one below.


Please Note: This xml example is NOT the exact setup I need. It is missing information and is subject to change due to updates to the database.



<?xml version="1.0" encoding="UTF-8"?>
<databaseConfiguration xmlns:xsi="http://ift.tt/ra1lAU"
xsi:noNamespaceSchemaLocation='database.xsd'>
<Software functionalDesignationHardware="OSFC" hwfin="2TC"
identname="vpu-coresoftware" partnumber="RCF33000670540T" repfin="250TC100SW1"
targetHardwareID="COLOSFCA350SEC">
<TargetModule functionalDesignationSoftware="SOFTWARE-VPU MERCURY OPERATIONAL PROGRAM"
installscriptpathname="/srv/vz/private/101/usr/applications/vpu/vpu-coresoftware/RCF33000670540T/install/RCF33000670540T.sh"
ata="46-12" status="7" swfin="250TCSW1" targetOSFC="VPU_MERCURY"
timestamp="1280000000" />
<TargetModule functionalDesignationSoftware="SOFTWARE-VPU VENUS OPERATIONAL PROGRAM"
installscriptpathname="/srv/vz/private/102/usr/applications/vpu/vpu-coresoftware/RCF33000670540T/install/RCF33000670540T.sh"
ata="46-12" status="7" swfin="250TCSW2" targetOSFC="VPU_VENUS"
timestamp="1280000000" />

<!-- Many more TargetModule sections... -->

</Software>
<Software functionalDesignationHardware="OSFC" hwfin="2TC"
identname="vpu-coresoftware" partnumber="RCF32000660540T" repfin="250TC100SW4"
targetHardwareID="COLOSFCA350SEC">
<TargetModule functionalDesignationSoftware="SOFTWARE-VPU JUPITER OPERATIONAL PROGRAM"
installscriptpathname="/srv/vz/private/104/usr/applications/vpu/vpu-coresoftware/RCF32000660540T/install/RCF32000660540T.sh"
ata="46-12" status="7" swfin="250TCSW4" targetOSFC="VPU_JUPITER"
timestamp="1280000000" />
</Software>

<!-- Many more Software sections... -->

<Hardware functionalDesignationHardware="OSFC" partnumber="983-9222-001"
hwfin="2TC" productName="OSFC" ser="SER" ata="46-12" />
<Hardware functionalDesignationHardware="IMCC-OSFC" partnumber="822-2913-001"
hwfin="7002TC" productName="IMCC" ser="SER" ata="46-12" />
<Hardware functionalDesignationHardware="IPSM-3" partnumber="822-2692-001"
hwfin="100TC3" productName="IPSM1" ser="SER" ata="46-14" />

<!-- Many more Hardware sections... -->

<Recipient identname="BS-CONTROL" queue="control.input"
recipientname="control.input" />
</databaseConfiguration>


This is how I'm accessing the database and retrieving information from each table:



import sqlite3

db = sqlite3.connect("dataload.db")
software = db.cursor().execute("SELECT type_, partnumber_, identname_, functionalDesignationHardware_, hwfin_, targetHardwareID_,"
"repfin_, amendment_ from SOFTWARE_")
softwareData = software.fetchall()


Questions:


How can I use the information from the database to create an xml file (similar to my example)?


Since I am accessing a database, the contents, size, and number of sections will be changing. Is it possible to use ElementTree to do this without knowing the exact structure/length of the xml file?


Is ElementTree even the best option? (Again, must be compatible with Python 2.6).


Please and thank you!


Aucun commentaire:

Enregistrer un commentaire