Package medite :: Package Utile :: Module codageXml
[hide private]
[frames] | no frames]

Source Code for Module medite.Utile.codageXml

  1  # -*- coding: iso-8859-1 -*- 
  2  # Copyright 20003 - 2008: Julien Bourdaillet (julien.bourdaillet@lip6.fr), Jean-Gabriel Ganascia (jean-gabriel.ganascia@lip6.fr) 
  3  # This file is part of MEDITE. 
  4  # 
  5  #    MEDITE is free software; you can redistribute it and/or modify 
  6  #    it under the terms of the GNU General Public License as published by 
  7  #    the Free Software Foundation; either version 2 of the License, or 
  8  #    (at your option) any later version. 
  9  # 
 10  #    MEDITE is distributed in the hope that it will be useful, 
 11  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  #    GNU General Public License for more details. 
 14  # 
 15  #    You should have received a copy of the GNU General Public License 
 16  #    along with Foobar; if not, write to the Free Software 
 17  #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 18  #---------------------------------------------------------------------------- 
 19  # Nom:          codageXml.py 
 20  # Objet:        fichier qui contient des methodes qui aide a la constructions 
 21  #               des fichiers xml 
 22  #---------------------------------------------------------------------------- 
 23  import Ft.Xml, Ft.Xml.Domlette 
 24  # ligne ci-dessous nécessaire car sinon lors du freeze avec cxfreeze il a besoin 
 25  # de cet import qui n'est pas ilmporté normalement 
 26  import Ft.Xml.XInclude 
 27  import Ft.Xml.XPath, Ft.Xml.XPath.ParsedPredicateList, Ft.Xml.XPath.ParsedAbbreviatedAbsoluteLocationPath 
 28  import Ft.Xml.XPath.ParsedAbbreviatedRelativeLocationPath 
 29  from xml.dom.minidom import * 
 30  #from xml.dom.pulldom import * 
 31  import sys 
 32   
 33  ## astuce pour l'encoding du fichier xml 
34 -def encodingXml(pChemin):
35 f = open(pChemin) 36 debut = f.read(23) 37 debut = debut[0:20] + ' encoding="iso-8859-1" ' +debut[20:] # iso-8859-1 38 reste = f.read() 39 ts = debut + reste 40 f.close() 41 f = open(pChemin,'w') 42 f.write(ts) 43 f.close()
44 ## pass 45 46 47 ## astuce pour l'encodage des noeuds des balises
48 -def encodingNode(pNode):
49 if not isinstance(pNode,unicode): 50 code = unicode(pNode, 'iso-8859-1') 51 else: code = pNode 52 return code
53 54 ## Methode pour parser un fichier xml accentue
55 -def lireDocXml(pChemin):
56 f = open(pChemin) 57 text = f.read() 58 f.close() 59 # print text 60 #textDoc = unicode(text,'iso-8859-1') 61 62 ficXML = xml.dom.minidom.parseString(text) 63 return ficXML
64 # return xml.dom.pulldom.parse(pChemin) 65 66 ## Methode pour parser un fichier xml accentue avec Ft.Xml
67 -def lireDocXmlFt(pChemin):
68 f = open(pChemin) 69 text = f.read() 70 f.close() 71 # print text 72 #textDoc = unicode(text,'iso-8859-1') 73 74 ficXML = Ft.Xml.Parse(text) 75 return ficXML
76 # return xml.dom.pulldom.parse(pChemin) 77 78 ## Methode pour parser un fichier xml en ne lisant que l'arbre des versions
79 -def lireDocXmlVersionTree(pChemin):
80 f = open(pChemin) 81 text = f.read() 82 f.close() 83 # on coupe après arbre pour ne garder que les infos de l'arbre des versions 84 pos = text.find('</arbre>') 85 text = text[:pos+8] + '</root>' 86 87 ficXML = xml.dom.minidom.parseString(text) 88 return ficXML
89 90 ## Methode pour ecrire un doc xml dans un fichier
91 -def ecrireDocXml(pChemin,pDocument):
92 f = open(pChemin,'w') 93 content = pDocument.toxml() 94 #d = SAX2DOM(pDocument) 95 #content = d.toxml() 96 # if isinstance(pDocument,xml.dom.minidom.Document): 97 # content = pDocument.toxml() 98 # else: #pullDom 99 # sys.stderr.write(str(type(pDocument))) 100 # sys.stderr.write(str(pDocument)) 101 # if pDocument == None: 102 # sys.stderr.write("none") 103 # sys.stderr.flush() 104 # for event, node in pDocument: 105 # sys.stderr.write("d\n") 106 # sys.stderr.write(str(event)+" "+str(node.nodeName)+" d \n") 107 # sys.stderr.flush() 108 # if (event=='START_DOCUMENT'): 109 # pDocument.expandNode(node) 110 # content = node.toxml() 111 # break 112 # elif (event!='START_DOCUMENT' and node.nodeName!='root'): 113 # pDocument.expandNode(node) 114 # sys.stderr.write("d"+node.toxml()+"\n") 115 # sys.stderr.flush() 116 117 f.write(content.encode('iso-8859-1')) 118 f.close() 119 encodingXml(pChemin)
120 121 122 ## Methode pour ecrire un doc xml dans un fichier avec Ft.Xml
123 -def ecrireDocXmlFt(pChemin,pDocument):
124 f = open(pChemin,'w') 125 #content = pDocument.toxml() 126 #f.write(content.encode('iso-8859-1')) 127 Ft.Xml.Domlette.Print(pDocument, stream=f, encoding='iso-8859-1') 128 f.close()
129 #encodingXml(pChemin) 130