any resouces for construct/parse xml string in classical asp using

C

c676228

Hi everyone,
I searched on the internet and didn't get exactly what I want. Do you have
any?
 
A

Anthony Jones

c676228 said:
Hi everyone,
I searched on the internet and didn't get exactly what I want. Do you have
any?

Use MSXML. To get a XML DOM use:-

Set oDOM = Server.CreateObject("MSXML2.DOMDocument.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-us/xmlsdk/html/e9da2722-7879-4e48-869c-7f16714e2824.asp
 
S

Steven Cheng[MSFT]

Thanks for Anthony's input.

Hi Betty,

As Anthony has mentioned, in classic ASP, you need to use COM component and
Microsoft has provided the built-in MSXML components which support standard
DOM api.

#MSXML
http://msdn.microsoft.com/library/en-us/xmlsdk/html/b24aafc2-bf1b-4702-bf1c-
b7ae3597eb0c.asp?frame=true


You can use it in visual basic, classic ASP, or client-side script code:

#Accessing XML Data using ASP
http://www.4guysfromrolla.com/webtech/101200-1.shtml


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

c676228

Hi Anthony and Steven,
Thank you both. I don't know what is MSXML2.DOMDocument.3.0 yet.
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? 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
 
S

Steven Cheng[MSFT]

Hello Betty,

MSXML2.DOMDocument.3.0 is just an ID of the DOMDocument class you used, it
is one of the MSXML components.

From your description, the "Access Denied" error occurs when you Save the
xmldocument into a file on disk, I think it is likely an NTFS permission
error. I think you can troubleshoot the issue through the following means:

** Explicitly specify a full path in the "Save" method(where the anonymous
acount has sufficient write permission) to see whether it works

** Try change the anonymous account to a more pownerful account (e.g admin
account) to see whether it can correctly save file

** For file acccess permission issue, you can use the filemon utility to
detect the detailed file access failture information:

http://www.sysinternals.com/utilities/filemon.html

Please feel free to let me know if you have any further finding.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Anthony Jones

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.
 
C

c676228

Thank you, Steven and Anthony, again.
Anthony, after I used xmldoc.save server.mapPath("filename"). It works
perfectly and it does not complain ianymore. I haven't look at your reply in
details yet. Just feel so blessed that you wrote so detailed email and
instruction. Can you send me an email to (e-mail address removed). One of my
friends is looking for a consultant.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top