"Subrato" <mukherjeesubrato@gmail.comwrote in message
news:1176992826.882993.141050@b75g2000hsg.googlegr oups.com...
Quote:
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
Quote:
'Sending
>
<%
RequestorFirstName = "Tony"
RequestorLastName = "Topper"
RequestorEmail = "tonytopper@topping.com"
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.
Quote:
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
Quote:
firstname = RequestorFirstName
lastname = RequestorLastName
|
Why??
Quote:
|
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
Quote:
|
xmlDoc = replace(xmlDoc,"[LastName]",lastname)
|
AddElem xmlDoc.documentElement, "FirstName", RequestorLastName
Quote:
|
xmlDoc = replace(xmlDoc,"[Line1]",RequestorAddress1)
|
and so on...
Quote:
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")
Quote:
>
' 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.
Quote:
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.
Quote:
objXMLDOM.async = False
Check = objXMLDOM.load(Request)
|
This is ok.
Quote:
|
Response.Write(Check & "11111")
|
Whats the "11111" about?
Quote:
|
objXMLDOM.Save("http://someotherserver:9099/xml/xmldoc.xml")
|
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.
Quote:
>
'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")
Quote:
if objLst.item(i).nodeName = "LastName" then
RequestorLastName = objLst.item(i).text
End if
|
RequestorFirstName = GetNodeText(oRoot, "LastName")
Quote:
if objLst.item(i).nodeName = "Email" then
RequestorEmail = objLst.item(i).text
End if
|
and so on..
Quote:
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.