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

VBScript Discussion :

VBScript ASP ET XMLDOM Modification attribut dans un fichier XML


Sujet :

VBScript

  1. #1
    Membre habitué
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2003
    Messages
    447
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2003
    Messages : 447
    Points : 173
    Points
    173
    Par défaut VBScript ASP ET XMLDOM Modification attribut dans un fichier XML
    Je voudrais savoir comment je pourrais faire pour modifier un attribut dans un fichier XML.

    Je parviens à lire toutes les informations de mon fichier, mais je ne parviens pas à y modifier un attribut, voici mon fichier XML :

    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
     
    <?xml version="1.0" encoding="UTF-8"?>
    <model_carte>
    	<nom Largeur="90" Hauteur="52" background="CarteDkRepro.gif">Modele simple</nom>
    	<elements Id="1">

    Je veux remplacer mon attribut "background"

  2. #2
    Expert éminent sénior


    Profil pro
    Inscrit en
    Juin 2003
    Messages
    14 008
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 14 008
    Points : 20 040
    Points
    20 040
    Par défaut
    s'il sagit d'un script.. .Vbs ... voir :

    Comment accéder aux attributs d'une balise ?

  3. #3
    Membre habitué
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2003
    Messages
    447
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2003
    Messages : 447
    Points : 173
    Points
    173
    Par défaut Mauvaise compréhension
    Je parviens très bien a lire tous les noeuds et aller pointer vers un noeuds bien défini mais je ne parviens pas a en changer un attribut.

  4. #4
    Expert éminent sénior


    Profil pro
    Inscrit en
    Juin 2003
    Messages
    14 008
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 14 008
    Points : 20 040
    Points
    20 040

  5. #5
    Membre habitué
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2003
    Messages
    447
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2003
    Messages : 447
    Points : 173
    Points
    173
    Par défaut Mon Attribut n'est pas changé
    Mon fichier est bien sauvegarder, mais aucune modification n'y a été apportées, voici mon code :

    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
     
     
     Dim objFSO 
     Dim xmlDoc,xmlPath,xmlNomModel,xmlTableUser,xmlTableAppli,node
     Set objFSO=Server.CreateObject("Scripting.FileSystemObject") 
     If objFSO.FileExists(GetDirWithoutLastDelimiter(REPWORK) &"\Cartes\CarteDeVisite.xml") Then
      set xmlDoc = nothing 
      set xmlDoc = Server.CreateObject("MSXML2.DOMDocument") 
      Test = xmlDoc.load(GetDirWithoutLastDelimiter(REPWORK) &"\Cartes\CarteDeVisite.xml")    
      If Test then 
       Set xmlPath = xmlDoc.documentElement
       If xmlPath.hasChildNodes() then
        If xmlPath.childnodes.item(0).text <> "" Then
         Set Noms = xmlPath.getElementsByTagName("nom")
         For Each Elem In Noms
          IdModel = Id
          Modele = Elem.childNodes(0).text
          ModeleLargeur = Elem.getAttribute("Largeur")
          ModeleHauteur = Elem.getAttribute("Hauteur")
     
          If Trim(ChoixModele) = Trim(IdModel) Then
           NomModel = Elem.childNodes(0).text
           CarteLargeur = Elem.getAttribute("Largeur")
           CarteHauteur = Elem.getAttribute("Hauteur")
           CarteImg = Elem.getAttribute("background")
     
           oElement = Replace(CarteImg, "CarteDeVisite\Logo\CarteDkRepro.gif", "CarteDkRepro.gif")
           Response.Write("oElement : "& oElement)
     
          End If
         Next
        End If 
       End If    
      End If   
     End If
     
     xmlDoc.save("D:\DocuRepro\Cartes\CarteDeVisite.xml")
     Set xmlDoc = Nothing

  6. #6
    Membre habitué
    Inscrit en
    Juin 2005
    Messages
    275
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 275
    Points : 186
    Points
    186
    Par défaut
    Etant donné que ton fichier XLM utilise UTF-8, peut-être pourrais-tu utiliser XSLT.

    Il existe en VBScript de quoi appliquer des modifications définies en XSLT sur un fichier XML.

    Ce n'est qu'une idée, je ne sais pas quelles sont les règles qui font que background doit changer. Toujours est-il que la sollution xslt fonctionnerait comme suit :

    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
    Sub lancerAdaptationXSLT(nomFichierInitial, nomFichierFinal, nomFichierXSLT)
        'déclaration et création des objets nécessaires à la fonction
        Dim oXml, oXsl, sortie, xslt, xslDoc, xmlDoc, xslProc
        Set oXml = CreateObject("MSXML2.DOMDocument.4.0")
        Set oXsl = CreateObject("MSXML2.DOMDocument.4.0")
        Set sortie = CreateObject("Scripting.FileSystemObject")
        Set fichierTemporaire = sortie.CreateTextFile(nomFichierFinal, True)
        oXml.validateOnParse = False
        oXml.async = False
        'chargement du fichier XML à transformer
        oXml.Load (nomFichierInitial) 'Attention au chemin du fichier
        If oXml.parseError.errorCode <> 0 Then
            MsgBox "erreur chargement XML : " & oXml.parseError.reason
        Else
            oXsl.async = False
            'chargement du fichier XSLT définissant les règles de transformation
            oXsl.Load (nomFichierXSLT)
            If oXsl.parseError.errorCode <> 0 Then
                MsgBox "erreur chargement XsL : " & oXsl.parseError.reason
            Else
            'application des transformations, que l'on écrit dans le fichier temporaire créé.
            fichierTemporaire.writeLine oXml.transformNode(oXsl.documentElement)
            fichierTemporaire.Close
            End If
        End If
        Set fichierTemporaire = Nothing
        Set sortie = Nothing
        Set oXsl = Nothing
        Set oXml = Nothing
    End Sub

  7. #7
    Membre habitué
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2003
    Messages
    447
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2003
    Messages : 447
    Points : 173
    Points
    173
    Par défaut Cela me paret bien compliqué
    Cela me paret bien compliqué à mettre en place uniquement pour pouvoir chnager un attribut, n'y a t'il pas d'autres solution quite même à utiliser autre chose que UTF-8.

  8. #8
    Membre habitué
    Inscrit en
    Juin 2005
    Messages
    275
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 275
    Points : 186
    Points
    186
    Par défaut
    Tu dis parcourir tous les noeuds pour pouvoir changer la valeur de certain de leurs attributs. Ce qui est exactement un des principes de XSLT.

    Je ne dois donc pas avoir bien compris ton objectif :

    - combien d'attributs veux-tu modifier dans ton ficher ? je pensais comprendre que plusieurs fois dans le même fichier, tu voulais changer background, mais la est peut etre mon erreur.
    - la structure de ton fichier XML est-elle figée ?

  9. #9
    Membre habitué
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2003
    Messages
    447
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2003
    Messages : 447
    Points : 173
    Points
    173
    Par défaut non désoler
    Non désoler tu as tres bien compris mon soucis , je veux bien changer un attribut dans mon fichier XML mon soucis est que je ne connais pas trop le XSLT avec ASP, mais peut etre pourrais tu me donner plus de détail pour réaliser mon changement d'attribut ?

  10. #10
    Membre habitué
    Inscrit en
    Juin 2005
    Messages
    275
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 275
    Points : 186
    Points
    186
    Par défaut
    Peut-être peux tu donner ton fichier XML pour qu'on y voit plus clair.

    Parles aussi un peu plus des valeurs que tu veux modifier dans tes attributs background. Est-ce toujours la même qui doit être mise à la place ?

  11. #11
    Membre habitué
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2003
    Messages
    447
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2003
    Messages : 447
    Points : 173
    Points
    173
    Par défaut Début du fichier Fichier XML
    Voici le début de mon fichier XML :

    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
     
    <?xml version="1.0" encoding="UTF-8"?>
    <model_carte>
    	<nom Largeur="90" Hauteur="52" background="CarteDkRepro.gif">Modele simple</nom>
    	<elements Id="1">
    		<element>
    			<Position PosX="500" PosY="200" Align="center" Presence="1">Nom - Prenom</Position>
    			<Police Italic="1" Gras="0" Souligne="0" Taille="10" TypePolice="Verdana"/>
    		</element>
                 </elements>
    </model_carte>

  12. #12
    Membre habitué
    Inscrit en
    Juin 2005
    Messages
    275
    Détails du profil
    Informations forums :
    Inscription : Juin 2005
    Messages : 275
    Points : 186
    Points
    186
    Par défaut
    Avec un xslt plutot simple, tu remplace toutes les balises background de ton fichier par une NOUVELLEVALEUR
    Code xml : 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
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:transform  version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    	<xsl:template match="node()" priority="1">
    		<xsl:copy>
    			<xsl:apply-templates select="node()|@*"/>
    		</xsl:copy>
    	</xsl:template>
    	<!-- règles pour les contenus de balise : on recopie -->
    	<xsl:template match="text()" priority="1">
    		<xsl:value-of select="."/> 
    	</xsl:template>   
    	<xsl:template match="@*">
    		<xsl:copy/>
    	</xsl:template>
                 <xsl:template match="@background" priority="2"> 
    		<xsl:attribute name="background">NOUVELLEVALEUR</xsl:attribute> 
    	</xsl:template>
    </xsl:transform>
    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <?xml version="1.0" encoding="UTF-8"?>
    <model_carte>
    	<nom Largeur="90" Hauteur="52" background="NOUVELLEVALEUR">Modele simple</nom>
    	<elements Id="1">
    		<element>
    			<Position PosX="500" PosY="200" Align="center" Presence="1">Nom - Prenom</Position>
    			<Police Italic="1" Gras="0" Souligne="0" Taille="10" TypePolice="Verdana"/>
    		</element>
                 </elements>
    </model_carte>
    A partir de la, tu devras juste y apporter la modification suffisante pour que seuls les valeurs background de model_carte/nom soient modifiés. Parce que dans l'état actuel des choses, la transformation se fera sur n'importe quel attribut BACKGROUND et ce, quelque soit la balise qui le contient. Ce qui est très ennuyeux...

    Le forum xslt répondra racilement à tes éventuels soucis

  13. #13
    Membre régulier
    Inscrit en
    Décembre 2005
    Messages
    114
    Détails du profil
    Informations forums :
    Inscription : Décembre 2005
    Messages : 114
    Points : 77
    Points
    77
    Par défaut
    bonjour
    essaye ca :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    xmlDoc=loadXMLDoc("books.xml")
    var x=xmlDoc.getElementsByTagName("book")
     
    for(i=0;i<x.length;i++)
      {
      x.item(i).setAttribute("category","bestseller")
      }
    essaye le lien : http://www.w3schools.com/dom/dom_nodes_set.asp
    il y a bcp d'infos utiles

    bonne lecture

Discussions similaires

  1. comment je pourrais modifier un attribut dans un fichier xml
    Par ryoussef19 dans le forum Visual C++
    Réponses: 6
    Dernier message: 24/08/2007, 14h16
  2. Réponses: 1
    Dernier message: 20/02/2007, 13h43
  3. Réponses: 3
    Dernier message: 29/06/2006, 16h54
  4. Affecter un attribut dans un fichier XML ou XSL
    Par LLaurent dans le forum XMLRAD
    Réponses: 7
    Dernier message: 05/06/2003, 14h04
  5. Lire un attribut dans un fichier XML en C++
    Par ti.k-nar dans le forum XML
    Réponses: 2
    Dernier message: 14/10/2002, 15h22

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