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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#-*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
'''
Created on 12 sept. 2017
@author: eric
'''
class Projet(object):
"""
Objet de type Projet
contient nom , auteur et type
"""
def __init__(self, nom , auteur , type):
self.nom = nom
self.auteur = auteur
self.type = type
def __str__(self):
return "\tnom: {o.nom}\n\tauteur: {o.auteur}\n".format(o=self)
class Amx(object):
"""
Objet de type Amx
contient une balise xnls ,
identificiation
date
type
x objet Projet
"""
def __init__(self, xnlns , identification , date , type , Projet=[]):
self.xnlns = xnlns
self.identification=identification
self.date= date
self.type = type
self.projet = Projet
def __str__(self):
return ("AMX")
#class Soap_env(object):
# def __init__(self, xmlns_soapenv ,xmlns_xsd , xmlns_xsi , Soap_body=[] ):
# self.soap_body =Soap_body
class Soap_Body(object):
def __init__(self, Amx=[]):
self.Amx = Amx
'''
Constructor
'''
def __str__(self):
return ("Soap_Body")
def parse_projet(node):
"""
<Projet>
<Nom>coco</Nom>
<Auteur>doudou</Auteur>
<type>2</type>
</Projet>
"""
p_nom = node.find("Nom").text
p_auteur = node.find("Auteur").text
p_type = node.find("type").text
return Projet(p_nom , p_auteur , p_type)
def parse_amx(node):
"""
<AMX xmlns="https://docs.python.org/3/library/xml.etree.elementtree.html">
<identification>TOTO</identification>
<date>2017-07-04T15:46:21</date>
<type>2</type>
<Projet> .....
</Projet>
</AMX>
"""
a_xnlns = node.find("xnlns").text
a_identification =node.find("identification").text
a_date =node.find("date").text
a_type = node.find("type").text
a_projet = [parse_projet(elt) for elt in node.findall("Projet")]
# recherche les balises Projet
return Amx(a_xnlns , a_identification , a_date , a_type ,a_projet)
def parse_soap_Body(node):
"""
<soapenv:Body>
<AMX xmlns="https://docs.python.org/3/library/xml.etree.elementtree.html">
...
</AMX>
</soapenv:Body>
"""
a_body =[parse_projet(elt) for elt in node.findall("soapenv:Body")]
return Soap_Body(a_body)
def parse_file(path):
with open(path, 'r') as fichier:
print(path)
tree = ET.ElementTree()
print(tree)
print(fichier)
tree.parse (fichier)
Soap_env = tree.getroot()
print(Soap_env)
return [parse_soap_Body(node) for node in Soap_env.findall("soapenv:Envelope")]
if __name__ == "__main__":
Soap_Bodys = parse_file("ex.xml")
print(Soap_Bodys)
for f in Soap_Bodys:
print(f) |
Partager