webForumDet fria alternativet

Skapa/redigara XML-dokument

2 svar · 376 visningar · startad av SPiN

SPiNMedlem sedan mars 20007 896 inlägg
#1

Halloj!

Jag har haft lite problem nu med en ActiveX-komponent som jag ska köra XML genom. @nders har gett mig ett par visa ord, men problemet kvarstår dock...

Jag vill genom komponenten kunna antingen skapa ett nytt eller redigara ett befintligt XML-dokument, där jag ska logga olika fel via en ASP-sida.

Subben som jag har problem med ser ut så här:

Public Sub Log(ByVal strLogFile As String, ByVal strError As String)
      On Error Resume Next

      Dim objXML As MSXML2.DOMDocument
      Dim objRoot As MSXML2.IXMLDOMElement
      Dim objNew As MSXML2.IXMLDOMElement
      Dim objEvent As MSXML2.IXMLDOMElement
      Dim bFileExisted As Boolean

      Set objXML = New MSXML2.DOMDocument
      objXML.async = False

      If FileExists(strLogFile) Then
            bFileExisted = True
            objXML.Load(strLogFile)
            Set objRoot = objXML.documentElement
      Else
            bFileExisted = False
            Set objRoot = objXML.createElement("EventLog")
            objXML.appendChild(objRoot)
      End If

      Set objNew = objXML.createElement("Event")
      objRoot.appendChild(objRoot)

      Set objEvent = objXML.createElement("File")
      objEvent.Text = objRequest.ServerVariables("SCRIPT_NAME")
      objNew.appendChild(objEvent)

      Set objEvent = objXML.createElement("Date")
      objEvent.Text = Now()
      objNew.appendChild(objEvent)

      Set objEvent = objXML.createElement("Error")
      objEvent.Text = strError
      objNew.appendChild(objEvent)

      If Not bFileExisted Then
            Dim objPI As MSXML2.IXMLDOMProcessingInstruction
            Set objPI = objXML.createProcessingInstruction("xml", "version='1.0' encoding='ISO-8859-1'")

            objXML.insertBefore objPI, objXML.childNodes(0)

            Set objPI = Nothing
      End If

      objXML.Save(strLogFile)

      Set objEvent = Nothing
      Set objNew = Nothing
      Set objRoot = Nothing
      Set objXML = Nothing

      If Err Then
            With objResponse
                  .Clear
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Write "Error<br>"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Write Err.Description
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .End
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End With
&nbsp; &nbsp; &nbsp; End If
End Sub

Felmeddelandet jag får är:
Object doesn't support this property or method

Men det hinner skapas ett XML-dokument, som innehåller process instruktionen, trots felmeddelandet.

------------------
SPiN, bjorne.w@telia.com

-- Tekniska framsteg är som en yxa i händerna på en kriminell galning / Albert Einstein --

[Redigerat av SPiN den 23 okt 2001]

SPiNMedlem sedan mars 20007 896 inlägg
#2

Hmm, jag har suttit och provat mig fram med olika sätt för att få det här att funka nu, och när jag gjorde som nedan fick jag inget felmeddelande, men inte heller skrevs det något till XML-filen ( förutom process instruktionen, såklart :OO ).

Public Sub TestLog(ByVal strLogFile As String)
&nbsp; &nbsp; &nbsp; On Error Resume Next

&nbsp; &nbsp; &nbsp; Dim objXML As MSXML2.DOMDocument30
&nbsp; &nbsp; &nbsp; Dim objRoot As MSXML2.IXMLDOMElement
&nbsp; &nbsp; &nbsp; Dim objEvent As MSXML2.IXMLDOMElement

&nbsp; &nbsp; &nbsp; Set objXML = New MSXML2.DOMDocument30
&nbsp; &nbsp; &nbsp; Set objRoot = objXML.createElement("rotElement")

&nbsp; &nbsp; &nbsp; set objEvent = objXML.createElement("eventElement")
&nbsp; &nbsp; &nbsp; objEvent.text = "event"
&nbsp; &nbsp; &nbsp; objRoot.appendChild(objEvent)

&nbsp; &nbsp; &nbsp; objXML.appendChild(objRoot)

&nbsp; &nbsp; &nbsp; Dim objPI As MSXML2.IXMLDOMProcessingInstruction
&nbsp; &nbsp; &nbsp; Set objPI = objXML.createProcessingInstruction("xml", "version='1.0' encoding='ISO-8859-1'")

&nbsp; &nbsp; &nbsp; objXML.insertBefore objPI, objXML.childNodes(0)

&nbsp; &nbsp; &nbsp; objXML.Save(strLogFile)

&nbsp; &nbsp; &nbsp; If Err Then
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; With objXML
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Clear
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Write Err.Description
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .End
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End With
&nbsp; &nbsp; &nbsp; End If
End Sub

Har också märkt att jag har skickat felaktiga objekt som parametrar till XMLDOM's metoder.
Bl.a.:
appendChild tar emot argument av typen IXMLDOMNode

Jag återkommer nog lite senare ikväll med fler upptäckter :e

------------------
SPiN, bjorne.w@telia.com

-- Tekniska framsteg är som en yxa i händerna på en kriminell galning / Albert Einstein --

SPiNMedlem sedan mars 20007 896 inlägg
#3

Nu ni, har jag löst det... Eller löst och löst, jag vet fortfarande inte som var fel :)

Jag struntade i att dimma mina variabler bara, och tog bort option explicit... Så nu har det hoppat igång. Jättetrevligt :)

128 ms totalt · 3 externa anrop · v20260731065814-full.beb2e261
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
124 ms — hämta tråd, inlägg och bilagor (db)