initial cpmmit

This commit is contained in:
Super_JK
2022-03-05 21:48:39 +01:00
commit 26c2570680
24 changed files with 399 additions and 0 deletions

17
python/TP1.py Executable file
View File

@@ -0,0 +1,17 @@
from xml.dom import minidom
doc = minidom.parse('../xml/TP1.xml')
print(doc.getElementsByTagName('Q1')[0].toxml()) # Représentation textuelle du sous-arbre
print(doc.getElementsByTagName('personne')[0].getAttribute('nom'))
print(doc.getElementsByTagName('personne')[0].getAttribute('prenom'))
print(doc.getElementsByTagName('personne')[0].childNodes)
print(doc.getElementsByTagName('personne')[0].childNodes[1].firstChild.data)
print(doc.getElementsByTagName('personne')[0].childNodes[3].firstChild.data)
print(doc.getElementsByTagName('personne')[0].childNodes[3].getAttribute('code'))
#print(doc.getElementsByTagName('sfq'))
#print(doc.getElementsByTagName('telephone').data)

39
python/TP2.py Normal file
View File

@@ -0,0 +1,39 @@
from os import listdir
from os.path import isdir, isfile
from xml.dom import minidom
doc = minidom.parse('../xml/TP1.xml')
dico = doc.getElementsByTagName('dictionary')[0]
def printDico(dico, past):
for child in dico.childNodes:
if child.nodeType == child.ELEMENT_NODE:
val = child.getAttribute('value')
if len(child.childNodes) == 0:
print(past + val)
else:
printDico(child, past + val + "-")
# printDico(dico,'')
def dirToXml(path, doc, el):
for elem in listdir(path):
path_ = path + "/" + elem
if isfile(path_):
node = doc.createElement("file")
node.attributes["name"] = elem
el.appendChild(node)
elif isdir(path_):
node = doc.createElement("dir")
node.attributes["name"] = elem
el.appendChild(node)
dirToXml(path_, doc, node)
root = minidom.Document()
xml = root.createElement('root')
root.appendChild(xml)
dirToXml("/", root, root.getElementsByTagName("root")[0])
print(root.toprettyxml())

55
python/personne.py Normal file
View File

@@ -0,0 +1,55 @@
from xml.dom import minidom
doc = minidom.parse('../xml/TP1.xml')
# Affiche l'arbre complet
#print(doc.toxml())
# Ou
#print(doc.toprettyxml())
# On récupère le noeud personne.
personne = doc.getElementsByTagName('personne')[0]
def getChildrenByTagName(doc, name):
"""Une fonction récupérant les fils directs d'un certain type."""
return [c for c in doc.childNodes
if c.nodeType == c.ELEMENT_NODE and c.tagName == name]
#personne = getChildrenByTagName(doc, 'personne')[0]
# Affiche le nom et le prenom de la personne
print(personne.getAttribute('nom'), personne.getAttribute('prenom'))
# Ou
print(personne.attributes['nom'].value, personne.attributes['prenom'].value)
# Affiche l'adresse email
email = doc.getElementsByTagName('email')[0]
email = getChildrenByTagName(personne, 'email')[0]
print(email.firstChild.data)
# Affiche l'adresse postale ainsi que le code postal
adresse = personne.getElementsByTagName('adresse')[0]
print(adresse.firstChild.data, adresse.getAttribute('code'))
# Acceder à un element inexistant
print(doc.getElementsByTagName('test'))
# Afficher le contenu de l'attribut data du telephone
# personne.getElementsByTagName('phone')[0].data
# Lister les methodes du noeud fils de email
print(dir(email.firstChild))
# Compter le nombre d'elements du noeud racine
print(len(doc.childNodes))
print(doc.childNodes)
print(doc.toxml())
# Modifier l'email et afficher le résultat
email.firstChild.data = "foo@bar.com"
print(doc.toxml())
# Modifier son numero de telephone
doc.getElementsByTagName('telephone')[0].attributes['numero'].value = '42'
print(doc.toxml())