I have an xml file that list jobs that need to be done. I want to be able to parse it with python. Here is my sample XML file
XML Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Jobs>
<Job name="Leo" type="upload">
<File name="Leo.csv" source="/leegin/leo/OU" destination="/leegin/leo/OU/scripts" archive="/leegin/leo/OU/history" date="1" del="1" stat="1"/>
<File name="Leo2.csv" source="/leegin/leo/OU" destination="/leegin/leo/OU/scripts" archive="/leegin/leo/OU/history" date="1" del="1" stat="1"/>
<Log name="Leo.txt" path="/leegin/leo/OU/log"/>
<Notify name="Leo Cruz" email="[email protected]"/>
<ftp port="21" proto="0" pasvmode="0" mode="0"/>
</Job>
<Job name="Manny" type="download">
<File name="Manny.csv" source="/leegin/leo/OU" destination="/leegin/leo/OU/scripts" archive="/leegin/leo/OU/history" date="1" del="1" stat="1"/>
<File name="Manny2.csv" source="/leegin/leo/OU" destination="/leegin/leo/OU/scripts" archive="/leegin/leo/OU/history" date="1" del="1" stat="1"/>
<Log name="Manny.txt" path="/leegin/leo/OU/log"/>
<Notify name="Manny Caparas" email="[email protected]"/>
<ftp port="21" proto="0" pasvmode="0" mode="0"/>
</Job>
<Job name="Joe" type="copy">
<File name="Joe.csv" source="/leegin/leo/OU" destination="/leegin/leo/OU/scripts" archive="/leegin/leo/OU/history" date="1" del="1" stat="1"/>
<File name="Joe2.csv" source="/leegin/leo/OU" destination="/leegin/leo/OU/scripts" archive="/leegin/leo/OU/history" date="1" del="1" stat="1"/>
<Log name="Joe.txt" path="/leegin/leo/OU/log"/>
<Notify name="Joe Gomez" email="[email protected]"/>
<ftp port="21" proto="0" pasvmode="0" mode="0"/>
</Job>
</Jobs>
Python Code:
#!/usr/bin/python2.6
import sys
import optparse
def main():
desc="""This script is used to setup and run an Automator job."""
parser = optparse.OptionParser()
parser.description = desc
parser.add_option('-j', dest='jobname', type='str', action='store', help='Name of job to execute', metavar='[JobName]')
parser.add_option('-v', dest='verbose', action='store_true', default=False, help='Used to view scripts debug information.')
(options, args) = parser.parse_args()
mandatory_options = ['jobname']
for m in mandatory_options:
if not options.__dict__[m]:
print 'Options -j is required.'
parser.print_help()
sys.exit(-1)
getjob(options.jobname)
def getjob(task):
from xml.etree import ElementTree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement
doc = ElementTree.parse('/opt/automize/template/jobs.xml')
Files = doc.findall("./Job/File")
for File in Files:
print File.attrib['name']
if __name__ == '__main__':
main()
Ok so what I am trying to do is to give the python script a job name, then have the script find the job in the XML file and extract only the part that pertains to the specific job.
So far I have been able to build a list of all jobs, or of all files. I have not been able to get it to do this for a specific job though. I would really appreciate some guidance with this matter.
ElementTreein 2.6 is incredibly slow on large files, so if your real data is significantly larger than your sample data, you should use the stdlib'scElementTreeinstead (or use a non-stdlib implementation).ISO-8859-1toutf-8without comment, I think it's more likely to cause confusion than anything, even if the OP does know the different rules for%-formatting vs.{}-formatting and mixed string types.