c676228 said:
Hi Anthony and Steven,
Thank you both. I don't know what is MSXML2.DOMDocument.3.0 yet.
It is an implementation of the MSXML DOM. With the Microsoft.XMLDOM progID
you don't know exactly what you will be getting, could be version 2, 2.6 or
3. Even if it does return a version 3 DOM it's behaviour isn't exactly the
same as using the version specific prog ID. I prefer know exactly which
implementation I'm creating.
I used the MSXML The following is a piece of code I played with. It works
fine and until I add a line
xmlDoc.Save("new.xml")
It gives an error message like this: msxml3.dll (0x80070005)
Access is denied.
/test.asp, line 58
I added IUSR_machinename to the security tab and allow the write permisson.
But it still doesn't work.
My purpose is save a xml before insert any data into database.
Can you tell me why?
Two reasons:-
Firstly you probably intended this file to be saved in the same folder as
the page which is running. In which case you should use:-
xmlDoc.Save Server.MapPath("new.xml")
Secondly as you have already suspected IUSR_ (if anonymous access is turned
on) needs to have write access to the folder. If you are using integrated
security then the user accessing the page needs to have write access.
Here is the code:
<%
call CustomersToXML
Function CustomersToXML()
Dim objXMLdoc
Dim objXMLRootElem
Set objXMLdoc=Server.CreateObject("Microsoft.XMLDOM")
With objXMLdoc
set objXMLRootElem=.createElement("Group")
.appendChild objXMLRootElem
Set GroupText=objxmlDoc.createTextNode("Group 10001")
objXMLRootElem.appendChild(GroupText)
set objXMLAttribGroupID=.createAttribute("GroupID")
objXMLAttribGroupID.nodeValue="1001"
objXMLRootElem.setAttributeNode objXMLAttribGroupID
End With
Set XMLdoc=Server.CreateObject("Microsoft.XMLDOM")
Set rootElement=xmlDoc.createElement("memo")
Set memoAttribute=xmlDoc.createAttribute("author")
Set memoAttributeText=xmlDoc.createTextNode("Pat Coleman")
Set toElement=xmlDoc.createElement("to")
Set toElementText=xmlDoc.createTextNode("Carole Poland")
memoAttribute.appendChild(memoAttributeText)
xmlDoc.appendChild(rootElement)
rootElement.setAttributeNode(memoAttribute)
rootElement.appendChild(toElement)
toElement.appendChild(toElementText)
xmlDoc.Save("new.xml") '------> error occurred
'xmlDoc.async = False
'xmlDoc.Load server.mappath("new.xml")
If (xmlDoc.parseError.errorCode <> 0) Then
Dim myErr
Set myErr = xmlDoc.parseError
Response.Write "You have error " & myErr.reason
Else
Response.Write xmlDoc.xml
End IF
Response.Write XMLDoc.documentElement.xml & "<br>"
Response.Write objXMLDoc.documentElement.xml & "<br>"
Response.Write XMLDoc.xml & "<br>"
Response.Write objXMLDoc.xml & "<br>"
set XMLdoc=Nothing
set objXMLDoc=Nothing
End Function
%>
Betty
Build XML DOMs in code can get a bit ugly. Using the text attribute of an
element and the setAttribute method would simplify things a lot. Also I
find it easier to create a root element by using the LoadXML method. The
following functions make it much easier to create a node heiarchy and value
carrying leaf nodes:-
Function AddElem(roParent, rsName, rvntValue)
Set AddElem = roParent.ownerDocument.createElement(rsName)
If Not IsNull(rvntValue) Then AddElem.text = rvntValue
roParent.appendChild AddElem
End Function
With all this in place your code can look like this:-
Function CustomersToXML()
Dim oDOMGroup
Dim oDOMMemo
Dim oRootElem
Set oDOMGroup = Server.CreateObject("MSXML2.DOMDocument.3.0")
oDOMGroup.loadXML "<Group />"
Set oRootElem = oDOM.documentElement
oRootElem.text = "Group 10001"
oRootElem.setAttribute "GroupID", "1001"
Set oDOMMemo = Server.CreateObject("MSXML2.DOMDocument.3.0")
oDOMMemo.loadXML "<memo />"
Set oRootElem = oDOMMemo.documentElement
oRootElem.setAttribute "author", "Pat Coleman"
AddElem oRootElem, "to", "Carole Poland"
oDOMMemo.Save Server.MapPath("new.xml")
'Removed superflous code
Response.Write Server.HTMLEncode(oDOMGroup.documentElement.xml) & "<br /><br
/>"
Response.Write Server.HTMLEncode(oDOMMemo.documentElement.xml) & "<br /><br
/>"
Response.Write Server.HTMLEncode(oDOMGroup.xml) & "<br /><br />"
Response.Write Server.HTMLEncode(oDOMMemo.xml) & "<br /><br />"
End Function
That said I suspect from earliers posts in this group that the true
destination of this XML is the OPENXML function in SQL Server right?
In that case I would expand the AddElem in order to handle dates correctly.
The standard locale specific formatting that a date is given when converted
to text does not parse back to the correct date in SQL Server. Here is an
expanded version:-
Function AddElem(roParent, rsName, rvntValue)
Set AddElem = roParent.ownerDocument.createElement(rsName)
If Not IsNull(rvntValue) Then
If VarType(rvntValue) = vbDate Then
AddElem.text = GetParseableDateTime(rvntValue)
Else
AddElem.text = rvntValue
End If
End If
roParent.appendChild AddElem
End Function
Function GetParseableDateTime(rdat)
GetParseableDateTime= FormatDate(rdat)
If TimeValue(rdat) <> 0 Then
GetParseableDateTime= GetParseableDateTime& " " & FormatTime(rdat)
End If
End Function
Function FormatDate(rdat)
FormatDate = PadText(DatePart("d", rdat), "00") & " " & _
MonthName(Month(rdat), true) & " " & _
DatePart("yyyy", rdat)
End Function
Function FormatTime(rdat)
FormatTime = PadText(DatePart("h", rdat), "00") & ":" & _
PadText(DatePart("n", rdat), "00") & ":" & _
PadText(DatePart("s", rdat), "00")
End Function
Function PadText(rsIn, rsSrc)
If Len(rsIn) >= Len(rsSrc) Then
PadText = rsIn
Else
PadText = Left(rsSrc, Len(rsSrc) - Len(rsIn)) & rsIn
End If
End Function
The GetParseableDateTime function returns a format of date time which is
parseable in an unambigous way by SQL Server as well as VB, VBScript and
Javascript not forgetting easily digested by a human as well (which is handy
when sending the XML in the other direction to the client for subsequent
display).
I suggest you stick these functions in an Include .asp file so you can use
them in multiple pages.
Here is a simple Master, Child usage example:-
Dim oDOM, oRoot
Dim oMaster, oChild
Dim i, j
Set oDOM = CreateObject("MSXML2.DOMDocument.3.0")
oDOM.loadXML("<root />")
Set oRoot = goDOM.documentElement
oRoot.setAttribute "CreateDate", GetParseableDateTime(Now)
For i = 1 To 2
Set oMaster = AddElem(oRoot, "master", null)
oMaster.setAttribute "ID", i
AddElem oMaster, "name", "Master " & i
For j = 1 To i * 2
Set oChild = AddElem(oMaster, "child", null)
oChild.setAttribute "ID", j
AddElem oChild, "name", "Child " & j & " of Master " & i
Next
Next
Cheers,
Anthony.