IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

Decomposé un fichier XML en plusieurs fichiers XML sous python


Sujet :

Python

  1. #1
    Candidat au Club
    Inscrit en
    Février 2007
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 8
    Points : 4
    Points
    4
    Par défaut Decomposé un fichier XML en plusieurs fichiers XML sous python
    bonjour tous le monde,

    voila mon code en XML :

    <offres>

    <offres-for>
    <nom>toto1</nom>
    <prenom>allo1</prenom>
    <adresse>
    <rue>12 rue 1</rue>
    <code-postal>0001</code-postal>
    </adresse>
    </offres-for>

    <offres-for>
    <nom>toto2</nom>
    <prenom>allo2</prenom>
    <adresse>
    <rue>12 rue 2</rue>
    <code-postal>0002</code-postal>
    </adresse>
    </offres-for>

    <offres-for>
    <nom>toto3</nom>
    <prenom>allo3</prenom>
    <adresse>
    <rue>12 rue 3</rue>
    <code-postal>0003</code-postal>
    </adresse>
    </offres-for>

    </offres>

    lire le fichier avec un parseurDOM
    pour chaque élement <offres-for> lui cré un fichier " offreX.xml "(offre1.xml,offre2.xml,...)

    Est-ce que quelqu'un pourrait m'aider?
    Merci d'avance.

  2. #2
    Membre expérimenté Avatar de pacificator
    Profil pro
    Inscrit en
    Août 2006
    Messages
    1 074
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 1 074
    Points : 1 728
    Points
    1 728
    Par défaut
    salut,
    je crois que tu as tout ce qu'il faut pour coder, je ne vois pas ton problème?
    "Etre conscient de la difficulté permet de l'éviter.."
    Lao-Tseu.

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    89
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 89
    Points : 107
    Points
    107

  4. #4
    Candidat au Club
    Inscrit en
    Février 2007
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 8
    Points : 4
    Points
    4
    Par défaut Merci pour tous
    bonjour,
    Merci pour le liens
    voila je suis debutant avec python et je voudrais avoir d autre liens pour que je puisse avancer le plus tot possible dans mon exercice helas j ai perdu beaucoup de temps

    Merci pour vos aide

  5. #5
    Candidat au Club
    Inscrit en
    Février 2007
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 8
    Points : 4
    Points
    4
    Par défaut aide
    Citation Envoyé par pacificator
    salut,
    je crois que tu as tout ce qu'il faut pour coder, je ne vois pas ton problème?
    bonjour ,
    voila je debute j ai pas une grande connaissance
    tu voudrais bien m aider a faire mon travail

    voila ce que j ai fait :
    ----------------------------------------------------------------

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    import sys, string
    from xml.sax import ContentHandler, make_parser
     
    class MonHandler(ContentHandler):
        # un constructeur qui initialise plusieurs variables pour notre application #
        def __init__(self,outfile):
            self.outfile = outfile
    	self.level = 0
        def startDocument(self):
            print "--Debut Document--"
     
        def EndDocument(self):
    	print "--Fin Document--"
     
        def startElement(self, name, attributes):
    	self.level +=1
    	self.printLevel()
    	self.outfile.write('Element: %s\n ' %name)
    	self.level +=2
        def endElement(self, name, attributes):
    	self.level -= 1
     
        def characters(self, data):
            pass
     
        def printLevel(self):
    	self.outfile.write(' ')
    def test(inFileName):
        outFile = sys.stdout
        handler = MonHandler(outfile)  # creation d un exemple de déroulement (Handler)
        parser = make_parser()  # création d un analyseur syntaxique
        parser.setContentHandler(handler)
        inFile = open(inFileName , 'r')
        parser.parse(infile)
        inFile.close()
     
        print 'offre-formation'
        for formation in formationList:
    	prin '   %s' % (formation, )
     
    def main():
        args = sys.argv[1:]
        if len(args) != 1:
    	print 'usage: python copieFichier.py base_offres.xml'
    	sys.exit(-1)
        test(args[0])
     
    if __name__ == '__main__':
        main()
    -----------------------------------------------------------------
    merci d avance

  6. #6
    Membre expérimenté Avatar de pacificator
    Profil pro
    Inscrit en
    Août 2006
    Messages
    1 074
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 1 074
    Points : 1 728
    Points
    1 728
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    from xml.dom import minidom
     
    dom = minidom.parse('offres.xml')
    sections = dom.getElementsByTagName('offres-for')
    for indice, section in enumerate(sections):
        open("offre%i.xml" % indice, 'w').write(section.toprettyxml())
    "Etre conscient de la difficulté permet de l'éviter.."
    Lao-Tseu.

  7. #7
    Candidat au Club
    Inscrit en
    Février 2007
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 8
    Points : 4
    Points
    4
    Par défaut
    ----------------------------------------------------------------------
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    from xml.dom import minidom
     
    dom = minidom.parse('offres.xml')
    sections = dom.getElementsByTagName('offres-for') 
    af = open("offres-atom.xml", 'w')
     
    #af.write("<?xml version=\""1.0"\" encoding=\""utf-8"\" ?>")
    af.write("<?xml version=\"" )
    af.write("1.0")
    af.write( "\" ")
    af.write("encoding=\"") 
    af.write("ytf-8")
    af.write( "\" ?>")
     
    af.write("<feed>")
     
    for indice, section in enumerate(sections):
     
         #af.write("<author>")
         #af.write("<name>")
         #af.write(section.getElementsByTagName('author'))
         #af.write("</name>")
         #af.write("<email>")
         #af.write(section.getElementsByTagName('email'))
         #af.write("</email>")
         #af.write("</author>")
     
         af.write("<link type=\"")
         af.write("text/html")
         af.write("\" />")
     
         af.write("<entry>")
         af.write("<div>")
     
         f = open("offre%i.xml" % indice, 'w')
         af.write("<title>")
         af.write(section.getElementsByTagName('intitule-action'))
         af.write("</title>")
         af.write("<link href=\"")
         af.write("offre%i.xml" %indice)
         af.write("\" />")
     
         af.write("<summary>")
     
         f.write("<lheo>")
         f.write("<offres>")
         f.write(section.toprettyxml(indent='  ', encoding='utf-8'))
         f.write("</offres>")
         f.write("</lheo>")
         af.write("</summary>")
     
         af.write("</div>")
         af.write("</entry>")
     
    af.write("</feed>")
    --------------------------------------------------------------

    une fois que j ai découpé le fichier offres.xml
    je crée un fichier offres-atom.xml qui contient les liens rapides vers les fichiers (affres1,offre2,...)
    la mise en page des fichiers offres.css

    problem:
    - j ai pas su afficher mon fichier offres-atom.xml dans une page html
    - comment faire pour que les diffirents fichiers crées prenne en compte la mise en page grace au fichier offres.css

    Merci d'avance pour votre aide .

Discussions similaires

  1. scinder hibetnate.cfg.xml en plusieurs fichiers XML
    Par pcouas dans le forum Hibernate
    Réponses: 0
    Dernier message: 06/07/2010, 13h08
  2. Réponses: 3
    Dernier message: 24/07/2009, 14h04
  3. Diviser un fichier XML en plusieurs fichiers XML avec XSLT
    Par bobkorn dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 18/04/2008, 12h13
  4. [XSLT]un fichier xsl et plusieurs fichiers xml
    Par akhtira dans le forum XSL/XSLT/XPATH
    Réponses: 1
    Dernier message: 04/02/2008, 11h03
  5. [XSLT] transfo d'un fichier XML en plusieurs fichiers XML
    Par doudou_rennes dans le forum XSL/XSLT/XPATH
    Réponses: 5
    Dernier message: 28/11/2006, 12h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo