Lecture dun fichier xml avec python
Salut, chers forumers!
je suis débutant en python et aimerais effectuer une lectuere d'un fichier xml dans un repertoire quelconque .
J'ai recu de mon directeur (je suis etudiant) le code python suivant:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
# -*- coding: latin1 -*-
"""Script for executing the Unit-Tests in the sourcen.
"""
import os
################################################################################
# BuildError
class BuildError(Exception):
"""Throw an exception, when there is an error while building.
"""
################################################################################
# shell_command
def shell_command(cmd):
"""Start a process and return the process-output as string.
"""
cmd2 = cmd + ' 2>&1'
p = os.popen(cmd2)
lines = []
line = p.readline()
while line:
lines.append(line)
line = p.readline()
try:
status = p.close()
except KeyboardInterrupt, e:
raise
except:
raise BuildError(''.join(lines))
if status:
raise BuildError(''.join(lines))
return lines
################################################################################
# execute_config
def execute_testscripts(path, config):
"""Seach the scripts with end .test.pl, .test.py, .test.bat in the transmitted files and execute it
If the returned status ist defferent to 0, the returned value will be add as Error.
A table ist returned, per testscript an incertion with the error-message.
"""
messages = {};
for root, dirs, files in os.walk(path):
if root.find('.svn') < 0: # nicht die Dateien aus den .svn-Verzeichnissen
for file in files:
cmd = None
if file.lower().endswith('.test.pl'):
cmd = 'perl.exe '
elif file.lower().endswith('.test.py'):
cmd = 'python.exe '
elif file.lower().endswith('.test.bat'):
cmd = 'cmd /C '
if cmd:
cmd += root + '\\' + file
print '%-30s ' % file,
try:
if 'Debug' in dirs:
shell_command('rmdir /S /Q ' + root + '\\Debug') # Delete all files from last tests
shell_command(cmd + ' %s' % config)
print 'ok'
messages[file] = ''
except BuildError, e:
print 'Fehler'
messages[file] = str(e)
return messages
################################################################################
# main
def main():
"""The "mainprogramm".
"""
import sys
rootpath = sys.argv[1]
messages = execute_testscripts(rootpath, 'Debug')
text = ''
has_errors = False
for file in sorted(messages.keys()):
status = 'ok'
if messages[file]:
status = 'Fehler'
has_errors = True
text += '%-30s %s\n' % (file, status)
if messages[file]:
text += '=' * 80 + '\n'
text += messages[file]
text += '=' * 80 + '\n\n'
dumpFile = open(sys.argv[2], 'w')
dumpFile.write(text)
dumpFile.close()
return has_errors
################################################################################
if __name__ == '__main__':
main() |
J'aimerais, à làide de ce code, lire le fichier xml suivant et l'afficher:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
<?xml version="1.0" encoding="UTF-8"?><!-- configurationsfile for testrunner.py --><run>
<!-- Global setting -->
<global>
<!-- Path to CA-Installation. This must be a Developer-Installation inclusivsourcetree ! -->
<!-- Path to Visual Studio 2008 -->
<devenv path="c:\Programme\Microsoft Visual Studio 9.0\Common7\IDE\devenv.com" version="2008"/>
<!-- Who shall be informed about compile-error? -->
<notify>
<email name="ghto@ioeiru.fr"/>
</notify>
</global>
<!-- Configuration for the den buildprocess and tests run-->
<configuration>
<!-- Pfad zu den Testfällen. -->
<testcasepath path="D:\Source\Project"/>
<!-- Path, where the protocols and archiv will be save -->
<output path="D:\Systemtests\CppUnitTestResults"/>
<!-- Tests only in the Debug or Releaseversion or in both? -->
<runtests>
<!--<debug /> -->
<release/>
</runtests>
<!-- Solutions to compile und VS-version used -->
<solutionfiles>
<solution path="Source\TesselationTest.sln" version="2008"/>
<solution path="Source\UtilitiesTest.sln" version="2008"/>
</solutionfiles>
</configuration>
<!-- Files to be tested -->
<tests>
<test assembly="BaseMathTest.dll" name="BaseMath" type="CppUnit">
<notify>
<email name="df@cm.fr"/>
</notify>
</test>
<test assembly="UtilitiesTest.dll" name="Utilities" type="CppUnit">
<notify>
<email name="trzr@ilkjgt.fr"/>
</notify>
</test>
</tests>
</run> |
Comment puis-je my prendre? quelqu'un peut t-il me donner une piste, voire un exemple ou bien me faire un code pour celà?
Merci d'avance
XML est tendance mais il faut un peu de travail.
Lire le chapitre 9 de "Dive Into Python".
Récupérer les exemples.
Bon courage
-W
Dive Into Python est livré avec dans l'aide Python de la distribution ActiveState.