xml positng and receiving code ?

S

Subrato

Hi,
I am very new to xml and I have this piece of code which I took off
a website. The situation is that on of the website, user files up a
form and it is submitted. On submission, the page should do a xml post
to my site. On receiving the xml on my end, I take that data and
insert it into sql server table. I dont know whats missing . Here is
the piece of code I use for my testing.

'Sending

<%
RequestorFirstName = "Tony"
RequestorLastName = "Topper"
RequestorEmail = "(e-mail address removed)"
RequestorAddress1 = "Top Address"
RequestorAddress2 = "High Lane"
RequestorCity = "New Haven"
RequestorState = "Connecitcut"
RequestorCountry = "USA"
RequestorZip = "06511"
RequestorContactFlag = "Yes"
RequestorInterestedBoat = "Laser,Nomad"
RequestorOwnership = "No"
RequestorActivities = "Saltwater Fishing, General Fitness"
RequestorBoatList = "Air Boats,Fish and Ski,Multi-Hull
Cruisers,Electric Boats"
RequestorPurchaseTimeLine = "As soon as possible"

Set xmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
xmlHttp.open "GET", "http://someserver:9099/example.xml", false
xmlHttp.send()
xmlDoc=xmlHttp.responseText
firstname = RequestorFirstName
lastname = RequestorLastName
xmlDoc = replace(xmlDoc,"[FirstName]",firstname)
xmlDoc = replace(xmlDoc,"[LastName]",lastname)
xmlDoc = replace(xmlDoc,"[Line1]",RequestorAddress1)
xmlDoc = replace(xmlDoc,"[Line2]",RequestorAddress2)
xmlDoc = replace(xmlDoc,"[City]",RequestorCity)
xmlDoc = replace(xmlDoc,"[StateProvinceCode]",RequestorState)
xmlDoc = replace(xmlDoc,"[PostalCode]",RequestorZip)
xmlDoc =
replace(xmlDoc,"[date]",FormatDateTime(date(),vbshortdate))
xmlDoc = replace(xmlDoc, "[ContactFlag]", RequestorContactFlag)
xmlDoc = replace(xmlDoc, "[InterestedInKnownBoat]",
RequestorInterestedBoat)
xmlDoc = replace(xmlDoc, "[Ownership]", RequestorOwnership)
xmlDoc = replace(xmlDoc, "[Activities]", RequestorActivities)
xmlDoc = replace(xmlDoc, "[BoatList]", RequestorBoatList)
xmlDoc = replace(xmlDoc, "[PurchaseTimeline]",
RequestorPurchaseTimeLine)

Set xml = Server.CreateObject("Microsoft.XMLHTTP")

' Notice the two changes in the next two lines:
xml.Open "POST", "http://someserver:9099/xmlimport.asp", False
xml.setRequestHeader "Content-Type", "application/x-www-form-
urlencoded"
xml.Send xmlDoc

'Response.Write( xml.responseText)
pos = ""
pos=InStr( xml.responseText,"<OrderRejectionMessage></
OrderRejectionMessage>")

'check rejection if error send email
if pos <> "" then
verror = "success"
else
verror = "error"
end if
Response.Write(verror)
%>

On receiving end

Set objXMLDOM = Server.CreateObject("MSXML2.DOMDocument")
objXMLDOM.setProperty "ServerHTTPRequest", True
objXMLDOM.async = False
Check = objXMLDOM.load(Request)
Response.Write(Check & "11111")
objXMLDOM.Save("http://someotherserver:9099/xml/xmldoc.xml")

'This just writes out the posted xml, but we could process it
here
Response.Write objXMLDOM



Set objLst = objXMLDOM.getElementsByTagName("*")

For i = 0 to (objLst.length) -1
if objLst.item(i).nodeName = "FirstName" then
RequestorFirstName = objLst.item(i).text
End if
if objLst.item(i).nodeName = "LastName" then
RequestorLastName = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Email" then
RequestorEmail = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Line1" then
RequestorAddress1 = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Line2" then
RequestorAddress2 = objLst.item(i).text
End if
if objLst.item(i).nodeName = "City" then
RequestorCity = objLst.item(i).text
End if
if objLst.item(i).nodeName = "StateProvinceCode" then
RequestorState = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Country" then
RequestorCountry = objLst.item(i).text
End if
if objLst.item(i).nodeName = "PostalCode" then
RequestorZip = objLst.item(i).text
End if
if objLst.item(i).nodeName = "ContactFlag" then
RequestorContactFlag = objLst.item(i).text
End if
if objLst.item(i).nodeName = "InterestedInKnownBoat" then
RequestorInterestedBoat = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Ownership" then
RequestorOwnership = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Activities" then
RequestorActivities = objLst.item(i).text
End if
if objLst.item(i).nodeName = "BoatList" then
RequestorBoatList = objLst.item(i).text
End if
if objLst.item(i).nodeName = "PurchaseTimeline" then
RequestorPurchaseTimeLine = objLst.item(i).text
End if
if objLst.item(i).nodeName = "date" then
RequestorDate = objLst.item(i).text
End if
Next
%>

I am unable to understand whats wrong or missing in this piece of code
 
A

Anthony Jones

Subrato said:
Hi,
I am very new to xml and I have this piece of code which I took off
a website. The situation is that on of the website, user files up a
form and it is submitted. On submission, the page should do a xml post
to my site. On receiving the xml on my end, I take that data and
insert it into sql server table. I dont know whats missing . Here is
the piece of code I use for my testing.

Comments in line
'Sending

<%
RequestorFirstName = "Tony"
RequestorLastName = "Topper"
RequestorEmail = "(e-mail address removed)"
RequestorAddress1 = "Top Address"
RequestorAddress2 = "High Lane"
RequestorCity = "New Haven"
RequestorState = "Connecitcut"
RequestorCountry = "USA"
RequestorZip = "06511"
RequestorContactFlag = "Yes"
RequestorInterestedBoat = "Laser,Nomad"
RequestorOwnership = "No"
RequestorActivities = "Saltwater Fishing, General Fitness"
RequestorBoatList = "Air Boats,Fish and Ski,Multi-Hull
Cruisers,Electric Boats"
RequestorPurchaseTimeLine = "As soon as possible"

Set xmlHttp = Server.CreateObject("Microsoft.XMLHTTP")

Don't use the XMLHTTP in ASP, its not thread safe, use
MSXML2.ServerXMLHTTP.3.0 instead.
xmlHttp.open "GET", "http://someserver:9099/example.xml", false
xmlHttp.send()
xmlDoc=xmlHttp.responseText

I doubt the above roundtrip to your server is necessary just use:-

Set xmlDoc = Server.CreateObject("MSXML2.DOMDocument.3.0")
xmlDoc.LoadXML "<root />"

If it is necessary for some other reason than to get the placemarks you are
using (which I'll correct below) then make sure the file is valid XML and
use:-

Set xmlDoc = xmlHttp.ResponseXML
firstname = RequestorFirstName
lastname = RequestorLastName
Why??

xmlDoc = replace(xmlDoc,"[FirstName]",firstname)

You should not attempt to create XML using a string. If you use the above
technique and the string being inserted happens to contain a < or & or some
other character of special meaning to XML the document will be corrupt.

Instead you should use a DOMDocument (as created above) to build your XML.
I use the following standard function to make building XML easier:-

Function AddElem(roParent, rsName, rvntValue)
Set AddElem = roParent.ownerDocument.createElement(rsName)
roParent.appendChild AddElem
If Not IsNull(rvntValue) AddElem.Text = rvntValue
End Function

With this function present the code above becomes:-

AddElem xmlDoc.documentElement, "FirstName", RequestorFirstName
xmlDoc = replace(xmlDoc,"[LastName]",lastname)

AddElem xmlDoc.documentElement, "FirstName", RequestorLastName
xmlDoc = replace(xmlDoc,"[Line1]",RequestorAddress1)

and so on...
xmlDoc = replace(xmlDoc,"[Line2]",RequestorAddress2)
xmlDoc = replace(xmlDoc,"[City]",RequestorCity)
xmlDoc = replace(xmlDoc,"[StateProvinceCode]",RequestorState)
xmlDoc = replace(xmlDoc,"[PostalCode]",RequestorZip)
xmlDoc =
replace(xmlDoc,"[date]",FormatDateTime(date(),vbshortdate))
xmlDoc = replace(xmlDoc, "[ContactFlag]", RequestorContactFlag)
xmlDoc = replace(xmlDoc, "[InterestedInKnownBoat]",
RequestorInterestedBoat)
xmlDoc = replace(xmlDoc, "[Ownership]", RequestorOwnership)
xmlDoc = replace(xmlDoc, "[Activities]", RequestorActivities)
xmlDoc = replace(xmlDoc, "[BoatList]", RequestorBoatList)
xmlDoc = replace(xmlDoc, "[PurchaseTimeline]",
RequestorPurchaseTimeLine)

Set xml = Server.CreateObject("Microsoft.XMLHTTP")

Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
' Notice the two changes in the next two lines:
xml.Open "POST", "http://someserver:9099/xmlimport.asp", False
xml.setRequestHeader "Content-Type", "application/x-www-form-
urlencoded"

Don't use the above header you're sending XML. Use:-

xml.setRequestHeader "Content-Type", "test/xml; CharSet=UTF-8"

OR don't bother with the header at all.
xml.Send xmlDoc

'Response.Write( xml.responseText)
pos = ""
pos=InStr( xml.responseText,"<OrderRejectionMessage></
OrderRejectionMessage>")

'check rejection if error send email
if pos <> "" then
verror = "success"
else
verror = "error"
end if
Response.Write(verror)
%>

On receiving end

Set objXMLDOM = Server.CreateObject("MSXML2.DOMDocument")
objXMLDOM.setProperty "ServerHTTPRequest", True

The ServerHTTPRequest property is not necessary. You are not making a
request; the content has already arrived and is in the Request object.
objXMLDOM.async = False
Check = objXMLDOM.load(Request)

This is ok.
Response.Write(Check & "11111")

Whats the "11111" about?

You cannot save to a URL. Is this yet another server to which the XML
should be passed to?
If so you should use the ServerXMLHTTP object to post the XML as you've done
form the origin.

BTW, don't use parentheses in method or procedure calls that aren't
returning any value.
'This just writes out the posted xml, but we could process it
here
Response.Write objXMLDOM



Set objLst = objXMLDOM.getElementsByTagName("*")

For i = 0 to (objLst.length) -1
if objLst.item(i).nodeName = "FirstName" then
RequestorFirstName = objLst.item(i).text
End if

I prefer to use another function to retrieve the text value of a path:-

Function GetNodeText(roContext, rsPath)
Dim oNode

Set oNode = roContext.SelectSingleNode(rsPath)

If Not oNode Is Nothing Then
GetNodeText = oNode.Text
Else
GetNodeText = Null
End If
End Function

Now you can use the following code to retrieve the values:-

Dim oRoot : Set oRoot = objXMLDOM.documentElement

RequestorFirstName = GetNodeText(oRoot, "FirstName")
if objLst.item(i).nodeName = "LastName" then
RequestorLastName = objLst.item(i).text
End if

RequestorFirstName = GetNodeText(oRoot, "LastName")
if objLst.item(i).nodeName = "Email" then
RequestorEmail = objLst.item(i).text
End if

and so on..
if objLst.item(i).nodeName = "Line1" then
RequestorAddress1 = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Line2" then
RequestorAddress2 = objLst.item(i).text
End if
if objLst.item(i).nodeName = "City" then
RequestorCity = objLst.item(i).text
End if
if objLst.item(i).nodeName = "StateProvinceCode" then
RequestorState = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Country" then
RequestorCountry = objLst.item(i).text
End if
if objLst.item(i).nodeName = "PostalCode" then
RequestorZip = objLst.item(i).text
End if
if objLst.item(i).nodeName = "ContactFlag" then
RequestorContactFlag = objLst.item(i).text
End if
if objLst.item(i).nodeName = "InterestedInKnownBoat" then
RequestorInterestedBoat = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Ownership" then
RequestorOwnership = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Activities" then
RequestorActivities = objLst.item(i).text
End if
if objLst.item(i).nodeName = "BoatList" then
RequestorBoatList = objLst.item(i).text
End if
if objLst.item(i).nodeName = "PurchaseTimeline" then
RequestorPurchaseTimeLine = objLst.item(i).text
End if
if objLst.item(i).nodeName = "date" then
RequestorDate = objLst.item(i).text
End if
Next
%>

I am unable to understand whats wrong or missing in this piece of code

Since you are using SQL Server consider passing the XML String to SQL Server
as parameter and using the OPENXML clause to retrieve the values from it.

One other note is on the date format used in the XML. I use dd-mmm-yyyy
(where mmm is the abbrevieated month name) which is parseable unambiguously
by VBScript, JScript and SQL Server.

Anthony.
 
V

Vanessa

hi Anthony,
Don't use the XMLHTTP in ASP, its not thread safe, use
MSXML2.ServerXMLHTTP.3.0 instead.

What do you mean by "XMLHTTP is not thread safe"? Actually I've been having
problem with XMLHTTP as it will freeze up the server (!!!) all the time. I've
been looking online to find an alternative method, so
MSXML2.ServerXMLHTTP.3.0 will be safe to use and won't lock up the server?
Please advise as I have searched for so long but with no luck...

Vanessa


Anthony Jones said:
Subrato said:
Hi,
I am very new to xml and I have this piece of code which I took off
a website. The situation is that on of the website, user files up a
form and it is submitted. On submission, the page should do a xml post
to my site. On receiving the xml on my end, I take that data and
insert it into sql server table. I dont know whats missing . Here is
the piece of code I use for my testing.

Comments in line
'Sending

<%
RequestorFirstName = "Tony"
RequestorLastName = "Topper"
RequestorEmail = "(e-mail address removed)"
RequestorAddress1 = "Top Address"
RequestorAddress2 = "High Lane"
RequestorCity = "New Haven"
RequestorState = "Connecitcut"
RequestorCountry = "USA"
RequestorZip = "06511"
RequestorContactFlag = "Yes"
RequestorInterestedBoat = "Laser,Nomad"
RequestorOwnership = "No"
RequestorActivities = "Saltwater Fishing, General Fitness"
RequestorBoatList = "Air Boats,Fish and Ski,Multi-Hull
Cruisers,Electric Boats"
RequestorPurchaseTimeLine = "As soon as possible"

Set xmlHttp = Server.CreateObject("Microsoft.XMLHTTP")

Don't use the XMLHTTP in ASP, its not thread safe, use
MSXML2.ServerXMLHTTP.3.0 instead.
xmlHttp.open "GET", "http://someserver:9099/example.xml", false
xmlHttp.send()
xmlDoc=xmlHttp.responseText

I doubt the above roundtrip to your server is necessary just use:-

Set xmlDoc = Server.CreateObject("MSXML2.DOMDocument.3.0")
xmlDoc.LoadXML "<root />"

If it is necessary for some other reason than to get the placemarks you are
using (which I'll correct below) then make sure the file is valid XML and
use:-

Set xmlDoc = xmlHttp.ResponseXML
firstname = RequestorFirstName
lastname = RequestorLastName
Why??

xmlDoc = replace(xmlDoc,"[FirstName]",firstname)

You should not attempt to create XML using a string. If you use the above
technique and the string being inserted happens to contain a < or & or some
other character of special meaning to XML the document will be corrupt.

Instead you should use a DOMDocument (as created above) to build your XML.
I use the following standard function to make building XML easier:-

Function AddElem(roParent, rsName, rvntValue)
Set AddElem = roParent.ownerDocument.createElement(rsName)
roParent.appendChild AddElem
If Not IsNull(rvntValue) AddElem.Text = rvntValue
End Function

With this function present the code above becomes:-

AddElem xmlDoc.documentElement, "FirstName", RequestorFirstName
xmlDoc = replace(xmlDoc,"[LastName]",lastname)

AddElem xmlDoc.documentElement, "FirstName", RequestorLastName
xmlDoc = replace(xmlDoc,"[Line1]",RequestorAddress1)

and so on...
xmlDoc = replace(xmlDoc,"[Line2]",RequestorAddress2)
xmlDoc = replace(xmlDoc,"[City]",RequestorCity)
xmlDoc = replace(xmlDoc,"[StateProvinceCode]",RequestorState)
xmlDoc = replace(xmlDoc,"[PostalCode]",RequestorZip)
xmlDoc =
replace(xmlDoc,"[date]",FormatDateTime(date(),vbshortdate))
xmlDoc = replace(xmlDoc, "[ContactFlag]", RequestorContactFlag)
xmlDoc = replace(xmlDoc, "[InterestedInKnownBoat]",
RequestorInterestedBoat)
xmlDoc = replace(xmlDoc, "[Ownership]", RequestorOwnership)
xmlDoc = replace(xmlDoc, "[Activities]", RequestorActivities)
xmlDoc = replace(xmlDoc, "[BoatList]", RequestorBoatList)
xmlDoc = replace(xmlDoc, "[PurchaseTimeline]",
RequestorPurchaseTimeLine)

Set xml = Server.CreateObject("Microsoft.XMLHTTP")

Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
' Notice the two changes in the next two lines:
xml.Open "POST", "http://someserver:9099/xmlimport.asp", False
xml.setRequestHeader "Content-Type", "application/x-www-form-
urlencoded"

Don't use the above header you're sending XML. Use:-

xml.setRequestHeader "Content-Type", "test/xml; CharSet=UTF-8"

OR don't bother with the header at all.
xml.Send xmlDoc

'Response.Write( xml.responseText)
pos = ""
pos=InStr( xml.responseText,"<OrderRejectionMessage></
OrderRejectionMessage>")

'check rejection if error send email
if pos <> "" then
verror = "success"
else
verror = "error"
end if
Response.Write(verror)
%>

On receiving end

Set objXMLDOM = Server.CreateObject("MSXML2.DOMDocument")
objXMLDOM.setProperty "ServerHTTPRequest", True

The ServerHTTPRequest property is not necessary. You are not making a
request; the content has already arrived and is in the Request object.
objXMLDOM.async = False
Check = objXMLDOM.load(Request)

This is ok.
Response.Write(Check & "11111")

Whats the "11111" about?

You cannot save to a URL. Is this yet another server to which the XML
should be passed to?
If so you should use the ServerXMLHTTP object to post the XML as you've done
form the origin.

BTW, don't use parentheses in method or procedure calls that aren't
returning any value.
'This just writes out the posted xml, but we could process it
here
Response.Write objXMLDOM



Set objLst = objXMLDOM.getElementsByTagName("*")

For i = 0 to (objLst.length) -1
if objLst.item(i).nodeName = "FirstName" then
RequestorFirstName = objLst.item(i).text
End if

I prefer to use another function to retrieve the text value of a path:-

Function GetNodeText(roContext, rsPath)
Dim oNode

Set oNode = roContext.SelectSingleNode(rsPath)

If Not oNode Is Nothing Then
GetNodeText = oNode.Text
Else
GetNodeText = Null
End If
End Function

Now you can use the following code to retrieve the values:-

Dim oRoot : Set oRoot = objXMLDOM.documentElement

RequestorFirstName = GetNodeText(oRoot, "FirstName")
if objLst.item(i).nodeName = "LastName" then
RequestorLastName = objLst.item(i).text
End if

RequestorFirstName = GetNodeText(oRoot, "LastName")
if objLst.item(i).nodeName = "Email" then
RequestorEmail = objLst.item(i).text
End if

and so on..
if objLst.item(i).nodeName = "Line1" then
RequestorAddress1 = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Line2" then
RequestorAddress2 = objLst.item(i).text
End if
if objLst.item(i).nodeName = "City" then
RequestorCity = objLst.item(i).text
End if
if objLst.item(i).nodeName = "StateProvinceCode" then
RequestorState = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Country" then
RequestorCountry = objLst.item(i).text
End if
if objLst.item(i).nodeName = "PostalCode" then
RequestorZip = objLst.item(i).text
End if
if objLst.item(i).nodeName = "ContactFlag" then
RequestorContactFlag = objLst.item(i).text
End if
if objLst.item(i).nodeName = "InterestedInKnownBoat" then
RequestorInterestedBoat = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Ownership" then
RequestorOwnership = objLst.item(i).text
End if
if objLst.item(i).nodeName = "Activities" then
RequestorActivities = objLst.item(i).text
End if
if objLst.item(i).nodeName = "BoatList" then
RequestorBoatList = objLst.item(i).text
End if
if objLst.item(i).nodeName = "PurchaseTimeline" then
RequestorPurchaseTimeLine = objLst.item(i).text
End if
if objLst.item(i).nodeName = "date" then
RequestorDate = objLst.item(i).text
End if
Next
%>

I am unable to understand whats wrong or missing in this piece of code

Since you are using SQL Server consider passing the XML String to SQL Server
as parameter and using the OPENXML clause to retrieve the values from it.

One other note is on the date format used in the XML. I use dd-mmm-yyyy
(where mmm is the abbrevieated month name) which is parseable unambiguously
by VBScript, JScript and SQL Server.

Anthony.
 
A

Anthony Jones

Vanessa said:
hi Anthony,


What do you mean by "XMLHTTP is not thread safe"?

XMLHTTP uses the same WinInet http stack that Internet explorer uses. This
stack is designed to be used by client applications running in a the context
of a user. It supports the managment of a cache which is stored in the
users Document And Settings folder and uses settings stored in the users
Registry Hive.

ServerXMLHTTP uses the WinHTTP stack which is a lighter API designed to
support server side applications. It does not use any User centric
resources.

WinInet when used in anger by multiple threads at the same time can have
unexpected behaviour. At least some of the functions in the WinInet API
will manipulate process wide resources without using synchronisation objects
that can protect it from multiple threads trying to maniputlate the same
resource at the same time.

WinHTTP is threadsafe in that it minimizes the process wide resources it
needs and if it does need to manipulate such resource then it will
synchronize them.

Actually I've been having
problem with XMLHTTP as it will freeze up the server (!!!) all the time. I've
been looking online to find an alternative method, so
MSXML2.ServerXMLHTTP.3.0 will be safe to use and won't lock up the server?
Please advise as I have searched for so long but with no luck...

I noticed you've posted this question as a separate thread. I'll answer
that there.
 

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,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top