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

ASP Discussion :

Générer un fichier XML


Sujet :

ASP

  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2005
    Messages
    352
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2005
    Messages : 352
    Par défaut Générer un fichier XML
    Bonsoir,

    J'aimerai savoir comment créer une page XML sur un serveur, à partir d'une page ASP ?

    Le type de page XML que j'aimerai obtenir est comme cela :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    <?xml version="1.0"?>
    <Calander>
    <info>
    <Data date="2007-05-18">Info 1</Data>
    </info>
    <info>
    <Data date="2007-05-23">Info 2</Data>
    </info>
    </Calander>
    Avec ce type d'élément.

    Merci de votre aide,

    Denis

  2. #2
    Membre chevronné
    Avatar de malbaladejo
    Profil pro
    Inscrit en
    Avril 2002
    Messages
    379
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Avril 2002
    Messages : 379
    Par défaut
    Tu peux faire ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <%
    Response.AddHeader "Content-Type","text/xml"
    %>
     
    <?xml version="1.0"?>
    <Calander>
    <info>
    <Data date="<%=date1%>"><%=Info1%></Data>
    </info>
    <info>
    <Data date="<%=date2%>"><%=Info2%></Data>
    </info>
    </Calander>

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2005
    Messages
    352
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2005
    Messages : 352
    Par défaut code correspondant mieux à mes besoins
    Re,

    Ce code correspondrait mieux à ce que j'ai besoin puisqu'il fait appel à une base de données, mais lorsque je fais l'essai en ligne, je n'obtiens rien au niveau du fichier xml crée :

    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
     
    <%
    '
     
    Set conn = Server.CreateObject("ADODB.Connection")
    connstring="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("./data/voyance.mdb")
    conn.Open connstring
     
    '
    SQL = "SELECT * FROM events"
    Set prod = Server.CreateObject("ADODB.Recordset")
    prod.Open sql, conn, 3, 3
    '
    if not(prod.eof) then
     
    '
    'response.write("<?xml version="""1.0""" ?>")"
    response.write("<?xml version='1.0' encoding='ISO-8859-1'?>")
     
    response.write("<calander>")
    '
    do while not prod.EOF
    '
    response.write("<info>")
    response.Write("<Url date=""" & prod.fields("date") & """ URL=""" & prod.fields("url") & """ />" )
    response.write("</info>")
    '
    prod.MoveNext
    Loop 
    end if
    '
    response.write("</calander>")
    '
    Set xmlDoc = server.CreateObject("Microsoft.XMLDOM")
    xmlDoc.preserveWhiteSpace=true
    xmlDoc.Save "C:\customer.xml"
    %>

  4. #4
    Expert confirmé
    Avatar de Immobilis
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mars 2004
    Messages
    6 559
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 559
    Par défaut
    Pour générer un fichier XML il faut utiliser
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    server.CreateObject("Microsoft.XMLDOM")
    Des response.write ne marchent pas
    "Winter is coming" (ma nouvelle page d'accueil)

  5. #5
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2005
    Messages
    352
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2005
    Messages : 352
    Par défaut
    Voici mon code, modifié avec informations que tu m'a donné :

    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
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
     
    <%
    '
     Dim objDom
     Dim objRoot
     Dim objField
     Dim objFieldValue
     Dim objattID
     Dim objattID2
     Dim objUrl
     
    Set conn = Server.CreateObject("ADODB.Connection")
    connstring="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("./data/voyance.mdb")
    conn.Open connstring
     
    '
    SQL = "SELECT * FROM events"
    Set prod = Server.CreateObject("ADODB.Recordset")
    prod.Open sql, conn, 3, 3
    '
    if not(prod.eof) then
     
    'Instantiate the Microsoft XMLDOM.
     Set objDom = server.CreateObject("Microsoft.XMLDOM")
     objDom.preserveWhiteSpace = True
     
     
     'Create your root element and append it to the XML document.
     Set objRoot = objDom.createElement("Calander")
     objDom.appendChild objRoot
    '
    do while not prod.EOF
    '
    Set objField = objDom.createElement("info")
    Set objUrl = objDom.createElement("Data")
    Set objattID = objDom.createAttribute("date")
    objattID.Text = prod.fields("date")
    Set objattID2 = objDom.createAttribute("date")
    objattID2.Text = prod.fields("url")
    objUrl.setAttributeNode objattID2
    objUrl.setAttributeNode objattID
     
    'Append the field element as a child of the root element.
    objRoot.appendChild objField 
    objField.appendChild objUrl
    '
    prod.MoveNext
    Loop 
    end if
    '
    'Create the xml processing instruction.
     Set objPI = objDom.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8' ")
     
     'Append the processing instruction to the XML document.
     objDom.insertBefore objPI, objDom.childNodes(0)
     
     
     'Save the XML document.
     objDom.save "C:\denague_date.xml"
     
     
     'Release all of your object references.
     Set objDom = Nothing
     Set objRoot = Nothing
     Set objField = Nothing
     Set objUrl = Nothing
     Set objattID = Nothing
     Set objattID2 = Nothing
     Set objPI = Nothing
     
    %>
    Qui me rend en code XML ceci :

    Code : 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"?>
    <Calander>
    <info>
    <Data date="12/05/2007"/>
    </info>
    <info>
    <Data date="14/06/2007"/>
    </info>
    </Calander>
    Alors que je cherche à obtenir ce type de document XML :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    <?xml version="1.0"?>
    <Calander>
    <info>
    <Data date="2007-05-18">Info 1</Data>
    </info>
    <info>
    <Data date="2007-05-23">Info 2</Data>
    </info>
    </Calander>
    Si quelqu'un peu m'aider sur la modif du code asp que j'ai pour obtenir le résultat que je recherche ? merci

    Denis

  6. #6
    Expert confirmé
    Avatar de Immobilis
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mars 2004
    Messages
    6 559
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 559
    Par défaut
    Bon, j'espere que j'ai bien tout compris.
    Avec ceci ça devrait aller un peu mieux sauf que cela n'ajoute pas d'attributs. Copie colle la totalité dans une page blanche et remplace simplement la requete SQL. Precise aussi correctement le chemin vers la base de données.
    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
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    <%@ LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <%Option explicit%>
     
    <%
    Dim strProvider 'Connection string
     
    call main
     
    sub main()
    	call connect
    	call XMLLauncher
    end sub
     
    Sub connect()
    	'Connects to the Access driver and Access database in the Inetpub directory where the database is saved
    	strProvider = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:\CHEMIN VERS LA BASE;"
    end sub
     
    sub XMLLauncher
    	dim rs, strqry, i
    	set rs = server.CreateObject("adodb.recordset")
    		strqry = "SELECT * FROM EAC_Dwnl;"
    		rs.open strqry, strprovider, 1, 1
    			response.Redirect(ConvertRStoXML(rs, "Noeud1", "Noeud2")) 
    		rs.close
    	set rs = nothing
    end sub
     
    function folderpath()
    	Dim fs,f
    	Set fs = Server.CreateObject("Scripting.FileSystemObject")
    		Set f = fs.GetFile(request.ServerVariables("PATH_TRANSLATED"))
    			folderpath = replace(request.ServerVariables("PATH_TRANSLATED"), f.name,"")
    		set f = nothing
    	set fs = nothing
    end function
     
    Function ConvertRStoXML(objRS, strTopLevelNodeName, strRowNodeName)
    	on error resume next
     
    	Dim objDom
    	Dim objRoot
    	Dim objField
    	Dim objFieldValue
    	Dim objcolName
    	Dim objattTabOrder
    	Dim objPI
    	Dim x
    	Dim objRSField
    	Dim objRow
    	Dim filename
     
    	objRS.movefirst
     
    	'Instantiate the Microsoft XMLDOM.
    	Set objDom = server.CreateObject("Microsoft.XMLDOM")
    	objDom.preserveWhiteSpace = True
     
    	'Create your root element and append it to the XML document.
    	Set objRoot = objDom.createElement(strTopLevelNodeName)
    	objDom.appendChild objRoot
     
    	Do While Not objRS.EOF
    		Set objRow = objDom.CreateElement(strRowNodeName)
     
    		For Each objRSField in objRS.Fields
    			Set objField = objDom.createElement(objRSField.Name)
    			objField.Text = objRSField.Value
     
    			objField.appendChild objFieldValue
     
    			objRow.appendChild objField
    		Next 
     
    		objRoot.appendChild objRow
     
    		objRS.MoveNext
    	Loop
     
    	Set objPI = objDom.createProcessingInstruction("xml", "version='1.0' encoding='iso-8859-1'")
    	objDom.insertBefore objPI, objDom.childNodes(0)
     
    	filename = "test.xml"
    	objDom.Save folderpath() & filename
    	ConvertRStoXML = filename
     
    	'Clean up...
    	Set filename = nothing
    	Set objDom = Nothing
    	Set objRoot = Nothing
    	Set objField = Nothing
    	Set objFieldValue = Nothing
    	Set objcolName = Nothing
    	Set objattTabOrder = Nothing
    	Set objPI = Nothing
    End Function
     
    %>
    "Winter is coming" (ma nouvelle page d'accueil)

  7. #7
    Membre expérimenté Avatar de lapanne
    Inscrit en
    Juin 2006
    Messages
    200
    Détails du profil
    Informations forums :
    Inscription : Juin 2006
    Messages : 200
    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
    Response.ContentType = "text/html"
    Response.AddHeader "Content-Disposition","attachment; filename=Annuaire.xml"
     
    Response.Write "<?xml version=""1.0"" encoding=""ISO-8859-1""?>" & vbCrLf & "<annuaire>" & vbCrLf
     
    '--------------------------------
    Set Utilisateurs = Server.CreateObject("ADODB.Connection")
    Utilisateurs.Open Session("Annuaire_ConnectionString"), Session("Utilisateurs_RuntimeUserName"), Session("Utilisateurs_RuntimePassword")
    Set cmdTemp = Server.CreateObject("ADODB.Command")
    Set RSLst = Server.CreateObject("ADODB.Recordset")
    cmdTemp.CommandText = "dbo.SP_xxxxx"
    cmdTemp.CommandType = 4
    cmdTemp.ActiveConnection = Utilisateurs
    RSLst.CursorType = 0
    RSLst.LockType = 1
    RSLst.Open(cmdTemp)
     
    Response.Flush
    While Not RSLst.EOF
    		Response.Write "<personne>" & vbCrLf
    		Response.Write "	<matricule>" & RSLst("id_pandore") & "</matricule>" & vbCrLf
    		Response.Write "  <nom>" & RSLst("nom") & "</nom>" & vbCrLf
    		Response.Write "  <prenom>" & RSLst("prenom") & "</prenom>" & vbCrLf
    		Response.Write "  <tel>" & RSLst("telephone") & "</tel>" & vbCrLf
    		Response.Write "  <extension>" & RSLst("extension_tel") & "</extension>" & vbCrLf
    		Response.Write "  <fax>" & RSLst("fax") & "</fax>" & vbCrLf
    		Response.Write "  <mobile>" & RSLst("mobile") & "</mobile>" & vbCrLf
    		Response.Write "  <mail>" & RSLst("mail_fast") & "</mail>" & vbCrLf
    		Response.Write "</personne>" & vbCrLf
    		RSLst.MoveNext
    Wend
    Response.Write "</annuaire>"
    Si ça peut t'aider voici une page asp de génération d'un fichier XML.

  8. #8
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2005
    Messages
    352
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2005
    Messages : 352
    Par défaut version minimal sortie xml dans page asp
    Bonsoir,

    J'utilise le code ci-dessous, pour avoir une des informations de type XML dans ma page asp :

    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
     
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-utf8-1">
    <title>Document sans nom</title>
    </head>
    <body>
    <%
    '
    'connection à la base de données
    Set oC = Server.CreateObject("ADODB.Connection")
    oC.Provider = "Microsoft.Jet.OLEDB.4.0"
    oC.Open "Data Source=" & Server.MapPath("./data/voyance.mdb") 
    'sélectionne toute les nouvelles
    SQL ="SELECT * FROM events"
     
    'création du Recordset
    Set oRS = CreateObject("ADODB.Recordset")
    oRS.Open SQL,oC,3,3
     
    'création du document XML  
    response.write "<?xml version=""1.0"" ?>"
    response.write("<Calander>")
     
    ' boucle pour collecter toutes les nouvelles
    ors.movefirst
    do while not ors.eof
    response.write("<info>")
    response.Write "<Data date=""" & ors("date") & """>" & ors("url") & "</Data>"
     
    response.write("</info>")
    ors.movenext
     
    Loop
     
    'on ferme le document XML
    response.write("</Calander> ")
     
    'on ferme la connection à la bdd
    Set oRS = Nothing
    oC.Close
    Set oC = Nothing
    %>  
     
    </body>
    </html>
    Ce qui me donne en sortie, lorsque j'affiche la source de ma page :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-utf8-1">
    <title>Document sans nom</title>
    </head>
    <body>
    <?xml version="1.0" ?><Calander><info><Data date="2007-05-18">denague.com</Data></info><info><Data date="2007-06-02">papy.com</Data></info></Calander>   
     
    </body>
    </html>
    Est-ce que c la version minimale de la sortie XML ? C'est à dire que j'aimerai, lorsque j' affiche la source de la page, ne plus avoir de code HTML, est-ce que cela est possible ?

    Merci d'avance,

    Denis

  9. #9
    Modérateur
    Avatar de roro06
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    1 480
    Détails du profil
    Informations personnelles :
    Âge : 56
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 1 480
    Par défaut
    Bonjour

    Tu dois d'abord enlever toutes les balises HTML, surtout celle-ci :
    <meta http-equiv="Content-Type" content="text/html; charset=iso-utf8-1">

    Puis rajouter en début de script :
    response.ContentType="text/XML"

    Ce faisant, n'en déplaise à immobilis, des response.write suffisent amplement

    cordialement


    N'oubliez pas de consulter les FAQ ASP et les cours et tutoriels ASP

    " La vie c'est quelque chose de très fort et de très beau.... La vie appartient a tous les vivants. It's both a dream and a feeling. C'est être ce que nous ne sommes pas sans le rester. La vie c'est mourir aussi....Et mourir c'est vraiment strong...c'est rester en vie au delà de la mort...Tous ceux qui sont morts n'ignorent pas de le savoir."
    (J.C. VanDamme, humoriste et philosophe belge . A moins que ce ne soit l'inverse ...)

    Chuck Norris comprend JC Van Damme.

  10. #10
    Expert confirmé
    Avatar de Immobilis
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mars 2004
    Messages
    6 559
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 559
    Par défaut
    Citation Envoyé par roro06
    Ce faisant, n'en déplaise à immobilis, des response.write suffisent amplement
    Pas de soucis
    "Winter is coming" (ma nouvelle page d'accueil)

Discussions similaires

  1. [PL/SQL] générer un fichier xml à partir du programme pl/sql
    Par moneyinthebank dans le forum PL/SQL
    Réponses: 2
    Dernier message: 15/06/2006, 17h09
  2. [DOMXML] Générer arborescence fichier XML
    Par gabychon dans le forum Format d'échange (XML, JSON...)
    Réponses: 3
    Dernier message: 21/04/2006, 10h59
  3. générer un fichier XML à partir d'un prog en C
    Par filou18fr dans le forum XML/XSL et SOAP
    Réponses: 10
    Dernier message: 22/02/2006, 16h09
  4. générer un fichier xml à partir xsl ou xslt
    Par sarah1 dans le forum XSL/XSLT/XPATH
    Réponses: 3
    Dernier message: 17/05/2005, 17h57
  5. Réponses: 2
    Dernier message: 27/05/2004, 00h40

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