"c676228" <be****@communi ty.nospamwrote in message
news:D2******** *************** ***********@mic rosoft.com...
Hi Anthony and Steven,
Thank you both. I don't know what is MSXML2.DOMDocum ent.3.0 yet.
It is an implementation of the MSXML DOM. With the Microsoft.XMLDO M 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("ne w.xml")
It gives an error message like this: msxml3.dll (0x80070005)
Access is denied.
/test.asp, line 58
I added IUSR_machinenam e 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=Serve r.CreateObject( "Microsoft.XMLD OM")
With objXMLdoc
set objXMLRootElem= .createElement( "Group")
.appendChild objXMLRootElem
Set GroupText=objxm lDoc.createText Node("Group 10001")
objXMLRootElem. appendChild(Gro upText)
set objXMLAttribGro upID=.createAtt ribute("GroupID ")
objXMLAttribGro upID.nodeValue= "1001"
objXMLRootElem. setAttributeNod e objXMLAttribGro upID
End With
Set XMLdoc=Server.C reateObject("Mi crosoft.XMLDOM" )
Set rootElement=xml Doc.createEleme nt("memo")
Set memoAttribute=x mlDoc.createAtt ribute("author" )
Set memoAttributeTe xt=xmlDoc.creat eTextNode("Pat Coleman")
Set toElement=xmlDo c.createElement ("to")
Set toElementText=x mlDoc.createTex tNode("Carole Poland")
memoAttribute.a ppendChild(memo AttributeText)
xmlDoc.appendCh ild(rootElement )
rootElement.set AttributeNode(m emoAttribute)
rootElement.app endChild(toElem ent)
toElement.appen dChild(toElemen tText)
xmlDoc.Save("ne w.xml") '------error occurred
'xmlDoc.async = False
'xmlDoc.Load server.mappath( "new.xml")
If (xmlDoc.parseEr ror.errorCode <0) Then
Dim myErr
Set myErr = xmlDoc.parseErr or
Response.Write "You have error " & myErr.reason
Else
Response.Write xmlDoc.xml
End IF
Response.Write XMLDoc.document Element.xml & "<br>"
Response.Write objXMLDoc.docum entElement.xml & "<br>"
Response.Write XMLDoc.xml & "<br>"
Response.Write objXMLDoc.xml & "<br>"
set XMLdoc=Nothing
set objXMLDoc=Nothi ng
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(roParen t, rsName, rvntValue)
Set AddElem = roParent.ownerD ocument.createE lement(rsName)
If Not IsNull(rvntValu e) Then AddElem.text = rvntValue
roParent.append Child 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.CreateOb ject("MSXML2.DO MDocument.3.0")
oDOMGroup.loadX ML "<Group />"
Set oRootElem = oDOM.documentEl ement
oRootElem.text = "Group 10001"
oRootElem.setAt tribute "GroupID", "1001"
Set oDOMMemo = Server.CreateOb ject("MSXML2.DO MDocument.3.0")
oDOMMemo.loadXM L "<memo />"
Set oRootElem = oDOMMemo.docume ntElement
oRootElem.setAt tribute "author", "Pat Coleman"
AddElem oRootElem, "to", "Carole Poland"
oDOMMemo.Save Server.MapPath( "new.xml")
'Removed superflous code
Response.Write Server.HTMLEnco de(oDOMGroup.do cumentElement.x ml) & "<br /><br
/>"
Response.Write Server.HTMLEnco de(oDOMMemo.doc umentElement.xm l) & "<br /><br
/>"
Response.Write Server.HTMLEnco de(oDOMGroup.xm l) & "<br /><br />"
Response.Write Server.HTMLEnco de(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(roParen t, rsName, rvntValue)
Set AddElem = roParent.ownerD ocument.createE lement(rsName)
If Not IsNull(rvntValu e) Then
If VarType(rvntVal ue) = vbDate Then
AddElem.text = GetParseableDat eTime(rvntValue )
Else
AddElem.text = rvntValue
End If
End If
roParent.append Child AddElem
End Function
Function GetParseableDat eTime(rdat)
GetParseableDat eTime= FormatDate(rdat )
If TimeValue(rdat) <0 Then
GetParseableDat eTime= GetParseableDat eTime& " " & FormatTime(rdat )
End If
End Function
Function FormatDate(rdat )
FormatDate = PadText(DatePar t("d", rdat), "00") & " " & _
MonthName(Month (rdat), true) & " " & _
DatePart("yyyy" , rdat)
End Function
Function FormatTime(rdat )
FormatTime = PadText(DatePar t("h", rdat), "00") & ":" & _
PadText(DatePar t("n", rdat), "00") & ":" & _
PadText(DatePar t("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 GetParseableDat eTime 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("M SXML2.DOMDocume nt.3.0")
oDOM.loadXML("< root />")
Set oRoot = goDOM.documentE lement
oRoot.setAttrib ute "CreateDate ", GetParseableDat eTime(Now)
For i = 1 To 2
Set oMaster = AddElem(oRoot, "master", null)
oMaster.setAttr ibute "ID", i
AddElem oMaster, "name", "Master " & i
For j = 1 To i * 2
Set oChild = AddElem(oMaster , "child", null)
oChild.setAttri bute "ID", j
AddElem oChild, "name", "Child " & j & " of Master " & i
Next
Next
Cheers,
Anthony.
>
"Anthony Jones" wrote:
"c676228" <be****@communi ty.nospamwrote in message
news:7D******** *************** ***********@mic rosoft.com...
Hi everyone,
I searched on the internet and didn't get exactly what I want. Do you
have
any?
--
Betty
Use MSXML. To get a XML DOM use:-
Set oDOM = Server.CreateOb ject("MSXML2.DO MDocument.3.0")
This is a reasonable starting point to learn how to used the DOM
http://www.w3schools.com/dom/default.asp
If you're not familiar with XML at all start with this:-
http://www.w3schools.com/xml/default.asp
then goto the DOM.
The official MS documentation is here:-
http://msdn.microsoft.com/library/en...16714e2824.asp