xml problème de reformattage
Bonjour
ce code marche mais voila soucis le fichier xml en sortie n'a pas la bonne présentation
. Je pars d'un fichier xml en je cherche a ajouter les balises soap en debut et fin de fichier.
Ce qui es dommage ici c'est que le fichier récupérer s'il est bien insérer n'a pas la bonne indentation.
si j'utilise --> dom.toprettyxml() il y a alors de ligne \n et des tabulations en exces.
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 101 102 103 104 105 106
| # coding: utf-8
import xlrd
import xml.dom.minidom
import string
import sys
import os
import datetime
import tkinter
from tkinter.filedialog import askopenfilename
'''
Created on 21 aout 2017
@author: moi
'''
def explorateur_window(repertoireinit):
""" Explorateur Windows """
root = tkinter.Tk()
#"Total-Flux_Non_Traitable_Flunet.txt"
filename = tkinter.filedialog.askopenfilename(
initialdir=repertoireinit,
title="Choisir votre fichier",
filetypes=(
("Fichier xml", "*.xml"),
("Tous type de fichier","*.*")
)
)
return filename
pass
def SoapIn(docxml):
"""Fonction de traitement.
ajouter les elements d'entête soapui
pour soumettre à soapui
"""
Soapenv_Envelope_ouvrant = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' + '\n'
Soapenv_body_ouvrant = '\t' + '<soapenv:Body>' + '\n'
Soapenv_Envelope_fermante = '\t' + '</soapenv:Body>'+ '\n'
Soapenv_body__fermante = '</soapenv:Envelope>'
rt_docxml = Soapenv_Envelope_ouvrant + Soapenv_body_ouvrant + docxml + Soapenv_Envelope_fermante + Soapenv_body__fermante
return rt_docxml
pass
def SoapOut(docxml):
"""Fonction de traitement.
ajouter les elements d'entête soapui
concernant le traitement résultat
"""
Soap_Envelope_ouvrant = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + '\n'
Soap_body_ouvrant = '\t' + '<soap:Body>' + '\n'
Soap_Envelope_fermante = '\t' + '</soap:Body>' + '\n'
Soap_body__fermante = '</soap:Envelope>'
rt_docxml = Soap_Envelope_ouvrant + Soap_body_ouvrant + docxml + Soap_Envelope_fermante + Soap_body__fermante
return rt_docxml
pass
def ajoutertype(source , destination):
"""Fonction de traitement.
Lit et traite le fichier source en une seule opétation
"""
toutesleslignes = source.read() # lecture du fichier
type = 1
if type == 1:
ligneecrire= SoapIn(toutesleslignes)
else:
ligneecrire= SoapOut(toutesleslignes)
print(ligneecrire)
# ecriture du fichier
dom = xml.dom.minidom.parseString(ligneecrire) # convertie le text en arbre xml DOM
#dom_xml = dom.toxml()
dom_xml = dom.toprettyxml(indent="", newl="", encoding=None)
# ecriture du fichier
destination.write(dom_xml)
pass
if __name__ == '__main__':
nom_fichier_path_in = explorateur_window ("C://") # Recherche via explorateur
if os.path.isfile(nom_fichier_path_in):
# ouverture du fichier
source = open(nom_fichier_path_in, "r")
# Ouverture du fichier destination
nom_fichier_in = os.path.basename(nom_fichier_path_in) # Retourne le dernier élément d'un chemin
nom_fichier_path = os.path.dirname(nom_fichier_path_in) # Retourne le dossier parent de l'élément
list_nf = os.path.split(nom_fichier_path_in) # Fractionne un chemin d'accès. Retourne un tuple
# Construction du fichier de sortie
nom_fichier_out = 'out_' + nom_fichier_in
nom_fichier_path_out = os.path.join(nom_fichier_path, nom_fichier_out)
destination = open(nom_fichier_path_out , "w") # ouverture du fichier de sortie
try:
if os.path.isfile(nom_fichier_path_in):
ajoutertype(source , destination)
finally:
if os.path.isfile(nom_fichier_path_in):
# Fermeture du fichier destination
destination.close()
# Fermerture du fichier source
source.close() |
le fichier xml
Code:
1 2 3 4 5
| <AMCREQ lire="livre1">
<identification>python à la maison</identification>
<auteur>python à la maison</auteur>
<datecreer>01/01/2016</datecreer>
</AMCREQ> |
le résultat
Code:
1 2 3 4 5 6 7 8
| <?xml version="1.0" ?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<AMCREQ lire="livre1">
<identification>python à la maison</identification>
<auteur>python à la maison</auteur>
<datecreer>01/01/2016</datecreer>
</AMCREQ> </soapenv:Body>
</soapenv:Envelope> |
j'aimerais obtenir
Code:
1 2 3 4 5 6 7 8 9
| <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<AMCREQ lire="livre1">
<identification>python à la maison</identification>
<auteur>python à la maison</auteur>
<datecreer>01/01/2016</datecreer>
</AMCREQ>
</soapenv:Body>
</soapenv:Envelope> |
une des solutions qui me viens à l'esprit serait de lire le fichier xml ligne par ligne .
est donc d'ajouter de but de chaque une tabulation '\t' .
Mais est-ce le plus pertinent?
si je ne m'abuse il y a
from lxml import etree
j'ai cru lire qu’après lecture du fichier format xml avec les commandes de xtree il serait possible
De lire le fichier brute.
De le parse . et d'ajouter de nouvelle balise