Parsing Inbound XML with Domino

I recently posted about talking back XML from Domino apps since I had never had to use the R6 LotusScript parser classes that Domino now has. In the same spirit, I thought I would post on how to parse inbound XML using the same classes.


Traditionally, most Domino developers used the Request_Content property/field to access the inbound POST stream and pull from that stream the parameters they need using a function like this:
<pre> Dim listValuePairs List As String Dim arrValuePairs As Variant strInput = docContext.request_content(0) arrValuePairs = Split(strInput,"&") Forall nv In arrValuePairs listValuePairs(Strleft(nv,"=")) = Strright(nv,"=") End Forall </pre>
This requires that the client be sending the stream as an HTTP post from, for example, an HTML form. But this does not address the use of XML in a POST since XML uses a more structured format.

In this example, we use a List object to store the resulting parameters extracted from the XML. The function requires the standard objects like DocumentContext and also uses the Request_Content property/field to get the POST content (the XML).

This is the general wrapper that calls the function:

Dim s As New NotesSession
Set docContext = s.DocumentContext
strInput = docContext.request_content(0)
if getEmployeeNameParam(s,strInput) then
	' -----proceed to use the listParams List
else
	' -----wrap-up the strOut variable and send it back to the client
end if

Here is the function:

------------Globals-------------------
Dim listParams List As String '< < list of parameters extracted from XML
Dim strOut As String '<< string to return to the client in the case of an error
Function getEmployeeNameParam(s As NotesSession, strInput As String) As Boolean
	On Error Goto errorCatch
	Dim domParser As NotesDOMParser
	Dim nodeDoc As NotesDOMDocumentNode
	Dim nodeList As NotesDOMNodeList
	Dim arrParams(2) As String
	Dim strTemp As String
	Dim x As Integer
	getEmployeeNameParam = False
	Set domParser = s.CreateDOMParser
	' the following are the tags to be found in the inbound xml string
	arrParams(0) = "emplastname"
	arrParams(1) = "empfirstname"
	arrParams(2) = "empmiddleinitial"
	' set the input to the xml text stream
	Call domParser.parse(strInput)
	Set nodeDoc = domParser.Document
	For x = 0 To 2 ' << loop through the param array and set paramlist elements
		Set nodeList = nodeDoc.GetElementsByTagName(arrParams(x))	
		' In my example, the emplastname parameter is required so I
		' want to make sure it is there. Some unexplained behavior
		' would sometimes allow numberOfEntries to show as 1 when
		' none existed so we double check everything to get the
		' values.  Looks redundant, and it is.
		If nodeList.NumberOfEntries > 0 Then
			‘ redundant code: had issues with an occasional
			‘ “null node” error popping up so the following
			‘ check is in place
			If nodeList.GetItem(1).isNull Then
				If x = 0 Then
					strOut = “The emplastname parameter is required!”
					Exit Function
				Else
					listParams(arrParams(x)) = “”
				End If
			Elseif nodeList.GetItem(1).hasChildNodes Then
				listParams(arrParams(x)) = nodeList.GetItem(1).FirstChild.NodeValue
			Else
				If x = 0 Then
					strOut = “The emplastname parameter is required”
					Exit Function
				Else
					listParams(arrParams(x)) = “”
				End If
			End If
		Else
			If x = 0 Then ‘ emplastname parameter was not included
				strOut = “The emplastname parameter is required.”
				Exit Function
			End If
			listParams(arrParams(x)) = “”
		End If
	Next
	‘ when all is said and done you will have the global listParams list populated.
	‘ to access a list element, refer to it with the notation: listParams(”emplastname”)
	getEmployeeNameParam = True
	Exit Function
errorCatch:
	strOut =  “Error in getuserInfo:getEmployeeNameParam in line: ” & Erl() & ” error$: ” & Error$
	Exit Function
End Function

Leave a Reply


Spam Karma 2 has sent 7705 comments to hell and 53 comments to purgatory. The total spam karma of this blog is -650. What's your karma?