Transformation CSV en XML

cs_mael974 Messages postés 108 Date d'inscription jeudi 2 juin 2005 Statut Membre Dernière intervention 10 mars 2011 - 6 juil. 2007 à 11:32
cs_MPi Messages postés 3877 Date d'inscription mardi 19 mars 2002 Statut Membre Dernière intervention 17 août 2018 - 7 juil. 2007 à 19:48
Bonjour,
Mon cas n'est pas facile a exposer ms je vais tenter detre clair!!
J'avais un besoin urgent de transformation dun fichier CSV en xml respectant le positionnement de chaque noeud . j'ai donc developpez (a la va vite) une macro excel. cette macro sappuie sur 2 feuilles excel: data et schema:
data : contiendra les donnees les unes a la suite des autres
schema: contiendra le nom des balises ainsi que leur positionnement!!

une structure de ce genre :<?xml:namespace prefix v ns "urn:schemas-microsoft-com:vml" /??><v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"> <v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /??><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype>
sera representé ainsi ds ma feuille excel:

voila pr le cote pratique. Cote technique le programme n'accepte que 3 sous elements ( 3 boucles). Je voudrais aujourdhuit rendre le programme + standard et faire une recursivite et avoir autant de ss elements quon veux.....s'est la que je fais appel a vous car je vois + tres clair et qun oeil exterieur pourrais p e maider a voir + clair.
Voici le bout de code qui nous interesse

'curseur pr la feuille des donnees
        data_indice = 1
        
        For i = 1 To 255

        profondeur = 0
        If Sheets("schema").Cells(1, i + 1) = "" Then
           
        'calcul de pronfondeur
        '------------------------------------
         i2 = i + 1
        'EOF = Sheets("schema").Cells(1, i2).End(xlToRight)
          While Sheets("schema").Cells(1, i2) = ""
           profondeur = profondeur + 1
           i2 = i2 + 1
          Wend
        '---------------------------------
        'fin traitement calcul - Clause echapatoire du programme - termine lexecution
           
          If profondeur > 3 Then
           txt = txt & "</" & RootElement & ">"
           GoTo finboucle
          End If
         '----------------------------------


          txt = txt & "<" & Sheets("schema").Cells(1, i) & ">"
               
          'Debut de construction de l'arbre
          '-----------------------------------
                 
                 
            'si  1 fils (profondeur 1)


                If profondeur = 1 Then
                   j = 1
                   c1 = i + 1
               
                     While Sheets("schema").Cells(j + 1, i + 1) <> ""
                      txt = txt & "<" & Sheets("schema").Cells(j + 1, c1) & "> " & Sheets("data").Cells(1, data_indice) & "</" & Sheets("schema").Cells(j + 1, c1) & ">"
                     j = j + 1
                     data_indice = data_indice + 1
                   
                     Wend
                     txt = txt & "</" & Sheets("schema").Cells(1, i) & ">"
                  End If
 
               'possibilite de recursivité??...a voir
               'cas profondeur 2 ou 3


                      If profondeur 2 Or profondeur 3 Then
                  
                    j = 1
                    c2 = i + 2
                    
                     While Sheets("schema").Cells(j + 1, i + 1) <> ""
                     
                   
                          'cas feuille dun noeud
                        While Sheets("schema").Cells(j + 1, i + 1) <> "" And Sheets("schema").Cells(j + 2, i + 2) = ""
                          txt = txt & "<" & Sheets("schema").Cells(j + 1, i + 1) & ">" & Sheets("data").Cells(1, data_indice) & "</" & Sheets("schema").Cells(j + 1, i + 1) & ">"
                          j = j + 1
                          data_indice = data_indice + 1
                        Wend
                         'EOF = Sheets("schema").Cells(j + 1, i + 1).End(xlDown)
                         'fin feuille du noeud
                         '------------------------------------------
                        
                         'cas profondeur 2 (fils avec une feuille)


                 
                 
                        If Sheets("schema").Cells(j + 1, i + 1) <> "" Then
                         txt = txt & "<" & Sheets("schema").Cells(j + 1, i + 1) & ">"
                         k = j
                        End If


                     'placer un for
                                        
                   
                       While Sheets("schema").Cells(k + 2, i + 2) <> ""
                    
                         'debut cas profondeur 3
line2:
                       '------------------------------------------------------------------------
                        
                         If Sheets("schema").Cells(k + 3, i + 3) <> "" Then
                          txt = txt & "<" & Sheets("schema").Cells(k + 2, i + 2) & ">"
                          m = k
                         End If
                        While Sheets("schema").Cells(m + 3, i + 3) <> ""
                          txt = txt & "<" & Sheets("schema").Cells(m + 3, i + 3) & ">" & Sheets("data").Cells(1, data_indice) & "</" & Sheets("schema").Cells(m + 3, i + 3) & ">"
                          m = m + 1
                          data_indice = data_indice + 1
                        Wend
                        
                         If Sheets("schema").Cells(k + 3, i + 3) <> "" Then
                          txt = txt & "</" & Sheets("schema").Cells(k + 2, i + 2) & ">"
                          k = m + 1
                         End If
                    
                        'instruction if et goto: retour profondeur 3 pour tester si il existe encore un element de profondeur 3
                         If Sheets("schema").Cells(k + 3, i + 3) <> "" Then
                         GoTo line2
                         End If
                        
                       'fin cas profondeur 3
                       '------------------------------------------------------------------------
                      
                        'reprise cas prondeur 2
                       
                        If Sheets("schema").Cells(k + 2, i + 2) <> "" Then
                         txt = txt & "<" & Sheets("schema").Cells(k + 2, i + 2) & ">" & Sheets("data").Cells(1, data_indice) & "</" & Sheets("schema").Cells(k + 2, i + 2) & ">"
                         k = k + 1
                         data_indice = data_indice + 1
                        End If
                     Wend
      
                        If Sheets("schema").Cells(j + 1, i + 1) <> "" Then
                         txt = txt & "</" & Sheets("schema").Cells(j + 1, i + 1) & ">"
                         j = k + 1
                        End If


                     Wend
                        txt = txt & "</" & Sheets("schema").Cells(1, i) & ">"
              End If
      End If
     
           
                       '-----------------------------------------------------------------------------


        'cas ou pas de fils
       If Sheets("schema").Cells(1, i + 1) <> "" Then
        txt = txt & "<" & Sheets("schema").Cells(1, i) & ">" & Sheets("data").Cells(1, data_indice) & "</" & Sheets("schema").Cells(1, i) & ">"
        data_indice = data_indice + 1
       End If
        i = i + profondeur
         
    Next i


finboucle:
 
 Print #FileNumber, txt
 Close #FileNumber

Voili voilou si vous voyez comment instaurer une boucle for pr rendre le prgmme recurssif merci de m'aider...cio

> MaEl <

2 réponses

cs_mael974 Messages postés 108 Date d'inscription jeudi 2 juin 2005 Statut Membre Dernière intervention 10 mars 2011
6 juil. 2007 à 14:24
JE sait pas si les images sont visibles!!! Sinon comment faire pr transferer les images????????
> MaEl <
0
cs_MPi Messages postés 3877 Date d'inscription mardi 19 mars 2002 Statut Membre Dernière intervention 17 août 2018 23
7 juil. 2007 à 19:48
Pour des images, je pense que tu doives passer par un serveur particulier et les inclure par lien HTML.
Pour ton problème, je ne vois pas... trop de code et trop abstrait...

MPi
0
Rejoignez-nous