XL 2016 [RESOLU] Suppression des caractères spéciaux dans un .xml

ivan27

XLDnaute Occasionnel
Bonjour le forum,

Je reçois régulièrement dans mes fichiers xml, des données avec des caractères ''fantaisistes'' que je n'arrive pas à intégrer automatiquement dans mon logiciel métier.
pour l'instant, les fichiers sont corrigés à la main.
Ci-dessous un code pour supprimer automatiquement certains caractères spéciaux qui ne sont pas acceptés par mon logiciel métier.

VB:
Sub SupprimerCaracteres()
Dim nf As String

nf = CreateObject("WScript.Shell").specialFolders("Desktop")

  Open nf & "\fichier1.xml" For Input As #1
  Open nf & "\fichier2.xml" For Output As #2
  Do While Not EOF(1)
    c = Input(1, #1)
 
    If c = "@" Then: c = " "
    If c = "ß" Then: c = "B"
    If c = "ä" Then: c = "a"
    If c = "ï" Then: c = "i"
    If c = "ö" Then: c = "o"
    If c = "ü" Then: c = "u"
    If c = "Ä" Then: c = "A"
    If c = "Ï" Then: c = "I"
    If c = "Ö" Then: c = "O"
    If c = "Ü" Then: c = "U"
    If c = "©" Then: c = " "
    If c = "'" Then: c = " "
    If c = "*" Then: c = " "
    If c = "²" Then: c = " "
    If c = "&" Then: c = " "
    If c = "~" Then: c = " "
    If c = "#" Then: c = " "
    If c = "{" Then: c = " "
    If c = "[" Then: c = " "
    If c = "|" Then: c = " "
    If c = "`" Then: c = " "
    If c = "\" Then: c = " "
    If c = "^" Then: c = " "
    If c = "]" Then: c = " "
    If c = "}" Then: c = " "
    If c = "+" Then: c = " "
    If c = "$" Then: c = " "
    If c = "£" Then: c = " "
    If c = "¤" Then: c = " "
    If c = "µ" Then: c = " "
    If c = "§" Then: c = " "
 
    Debug.Print c
    Print #2, c;
  Loop
  Close #1, #2
End Sub

Il arrive souvent que je trouve dans les champs des caractères spéciaux tels que : ?, !, /
Le problème est que ces caractères sont dans la structure du xml et ne peuvent pas être supprimés sans compromettre l'intégrité du fichier.
Exemple de fichier xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- XML Descriptif -->
<!--DOCTYPE Expeditions -->
-<Expeditions>
-<Entete-Message>
<Code-GTF/>
<Date-Expedition>20180222</Date-Expedition>
<Creation-Time>20180222 15:22:34</Creation-Time>
</Entete-Message>
-<Expedition>
<Type-Expedition>EXP</Type-Expedition>
<Ref-Expedition>896321318</Ref-Expedition>
<Num-Recepisse/>
<RFF-Expedition>0001690032</RFF-Expedition>
-<Expediteur>
<Code-Expediteur/>
<Nom-Expediteur>NOM DE L'EXPEDITEUR</Nom-Expediteur>
<Ad1-Expediteur>TEST 18</Ad1-Expediteur>
<Ad2-Expediteur/>
<Pays-Expediteur>PL</Pays-Expediteur>
<CP-Expediteur>67-100</CP-Expediteur>
<Ville-Expediteur>EXPEDITEUR !</Ville-Expediteur>
</Expediteur>
-<Destinataire>
<Code-Destinataire/>
<Nom-Destinataire>NOM DU DESTINATAIRE</Nom-Destinataire>
<Ad1-Destinataire>25 AVENUE DE LA REPUBLIQUE</Ad1-Destinataire>
<Ad2-Destinataire/>
<Pays-Destinataire>FR</Pays-Destinataire>
<CP-Destinataire>75000</CP-Destinataire>
<Ville-Destinataire>PARIS</Ville-Destinataire>
</Destinataire>
-<Premiere-PF>
<Code-1PF>Pas de code reseau pour le trafic de sous-traitance position recep XXXXXXXXXX</Code-1PF>
<Date-1PF>20180222 00:00</Date-1PF>
</Premiere-PF>
<Code-Produit>M?ES</Code-Produit>
<Nb-Palette>0</Nb-Palette>
<Nb-Paleur>6</Nb-Paleur>
<Nb-Palsol>0</Nb-Palsol>
<Nb-EqPal>0</Nb-EqPal>
<Metre-Plancher>0</Metre-Plancher>
<Poids>3600,000</Poids>
<Nb-UM>6</Nb-UM>
<Quantite/>
<Volume>0</Volume>
<Code-Contrat-Vente/>
<Code-Service>N</Code-Service>
<Rdv> </Rdv>
<Max-Liv> </Max-Liv>
<Inst-Speciales>TEXTE LIBRE</Inst-Speciales>
<Code-Pd>N</Code-Pd>
<Code-PF-Imp>Pas de code reseau pour //le trafic// de sous-traitance position RecepXXXXXXXXXX</Code-PF-Imp>
<Cpte-Fact/>
<Ex-TVA>E</Ex-TVA>
<Nb-EqPal-Dgs>0</Nb-EqPal-Dgs>
<Metre-Plancher-Dgs>0</Metre-Plancher-Dgs>
<ADRs> </ADRs>
<Doc-Douane>N</Doc-Douane>
<Acquis>N</Acquis>
-<Cb-UMs>
<Cb-UM>123456??312D221006</Cb-UM>
<Cb-UM>123456153312222003</Cb-UM>
<Cb-UM>235692153312223000</Cb-UM>
<Cb-UM>P2222215?312224005</Cb-UM>
<Cb-UM>PL2309153312225002</Cb-UM>
<Cb-UM>589632153312226007</Cb-UM>
</Cb-UMs>
</Expedition>
</Expeditions>

Ma question :
Comment supprimer également ces caractères sans compromettre la structure du xml ?
L'un de vous aurait-il une solution ? Il faudrait que le code fonctionne uniquement entre > et < ou alors convertir le xml en txt, le corriger et refaire un xml.
Bien cordialement et bonne journée à tous
Ivan
 

vgendron

XLDnaute Barbatruc
Bonjour

une idée d'algo..
pour chaque caractère du fichier
VB:
Bypasser=Faux 
c = Input(1, #1)
si c= "<"   then Bypasser=vrai 'début d'un champ XML
si c=">" then Bypasser=faux 'Fin d'un champ XML
si Bypasser=faux then 
    If c = "@" Then: c = " "
   If c = "ß" Then: c = "B"
   If c = "ä" Then: c = "a"
   If c = "ï" Then: c = "i"
   If c = "ö" Then: c = "o"
   If c = "ü" Then: c = "u"
   If c = "Ä" Then: c = "A"
   If c = "Ï" Then: c = "I"
   If c = "Ö" Then: c = "O"
   If c = "Ü" Then: c = "U"
   If c = "©" Then: c = " "
   If c = "'" Then: c = " "
   If c = "*" Then: c = " "
   If c = "²" Then: c = " "
   If c = "&" Then: c = " "
   If c = "~" Then: c = " "
   If c = "#" Then: c = " "
   If c = "{" Then: c = " "
   If c = "[" Then: c = " "
   If c = "|" Then: c = " "
   If c = "`" Then: c = " "
   If c = "\" Then: c = " "
   If c = "^" Then: c = " "
   If c = "]" Then: c = " "
   If c = "}" Then: c = " "
   If c = "+" Then: c = " "
   If c = "$" Then: c = " "
   If c = "£" Then: c = " "
   If c = "¤" Then: c = " "
   If c = "µ" Then: c = " "
   If c = "§" Then: c = " "
fin si
 

ivan27

XLDnaute Occasionnel
Rebonjour le forum, vgendron,

Je te remercie pour ta proposition mais ça ne semble pas fonctionner.

If c = "<" Then: Bypasser = vrai 'début d'un champ XML
If c = ">" Then: Bypasser = faux 'Fin d'un champ XML
If Bypasser = faux Then
If c = "?" Then: c = " "

et cela donne :

< xml version="1.0" encoding="UTF-8" >

Bien cordialement,

Ivan
 

vgendron

XLDnaute Barbatruc
euh.. j'ai dit "Algo".. donc forcément.. vrai et faux ne marchent pas.. il faut traduire en langage.. True False.....
avec ceci..?
VB:
Sub SupprimerCaracteres()
Dim nf As String

nf = CreateObject("WScript.Shell").specialFolders("Desktop")

  Open nf & "\fichier1.xml" For Input As #1
  Open nf & "\fichier2.xml" For Output As #2
  Do While Not EOF(1)
    c = Input(1, #1)
    If c = "<" Then Bypasser = True
    If c = ">" Then Bypasser = False
    If Bypasser = False Then
        If c = "@" Then: c = " "
        If c = "ß" Then: c = "B"
        If c = "ä" Then: c = "a"
        If c = "ï" Then: c = "i"
        If c = "ö" Then: c = "o"
        If c = "ü" Then: c = "u"
        If c = "Ä" Then: c = "A"
        If c = "Ï" Then: c = "I"
        If c = "Ö" Then: c = "O"
        If c = "Ü" Then: c = "U"
        If c = "©" Then: c = " "
        If c = "'" Then: c = " "
        If c = "*" Then: c = " "
        If c = "²" Then: c = " "
        If c = "&" Then: c = " "
        If c = "~" Then: c = " "
        If c = "#" Then: c = " "
        If c = "{" Then: c = " "
        If c = "[" Then: c = " "
        If c = "|" Then: c = " "
        If c = "`" Then: c = " "
        If c = "\" Then: c = " "
        If c = "^" Then: c = " "
        If c = "]" Then: c = " "
        If c = "}" Then: c = " "
        If c = "+" Then: c = " "
        If c = "$" Then: c = " "
        If c = "£" Then: c = " "
        If c = "¤" Then: c = " "
        If c = "µ" Then: c = " "
        If c = "§" Then: c = " "
        Debug.Print c
    End If
    Print #2, c;
  Loop
  Close #1, #2
End Sub
 

Statistiques des forums

Discussions
312 184
Messages
2 086 007
Membres
103 088
dernier inscrit
Psodam