Domino中使用Java访问Webservice(四)
在Lotus Designer开发工具中可以使用Java编写Webservice以及访问Webservice。这里只介绍一下如何访问Websercice。(续)
绝大多数调用Webservice会返回一个结果到客户端,其结果也是XML数据,这样我们要获取XML中指定元素的数据,就必须解析返回的XML数据。为此,在Lotus中,使用LotusScript编写一个解析XML数据格式的功能函数,返回的是以指定节点(参数:sNodeName)为根节点的元素值列表。代码如下:
Dim retList List as String
'把Scam返回的xml字符串解析为List列表,并且存放到retList全局变量中
Sub parseXmlToList(session As NotesSession,sXml As String,sNodeName As String,docApp As NotesDocument)
Dim domParser As NotesDOMParser
Dim domDocumentNode As NotesDOMDocumentNode
Dim domNodeList As NotesDOMNodeList
Dim domNode, childDomNode, lastDomNode As NotesDOMNode
Dim nNodeList, nChildNodeList As Integer
On Error Goto getListErr
'把sXml字符串解析为Node节点
Set domParser = session.CreateDOMParser(sXml)
Call domParser.Process
Set domDocumentNode = domParser.Document
Set domNodeList = domDocumentNode.GetElementsByTagName(sNodeName)
nNodeList = domNodeList.NumberOfEntries
'把每一个节点信息存放到retList全局变量中
Erase retList '清空retList内容
If nNodeList > 0 Then
For i=1 To nNodeList
Set domNode = domNodeList.GetItem(i) '获取sNodeName节点
If domNode.HasChildNodes Then
nChildNodeList = domNode.NumberOfChildNodes '获取所有item节点集合
Set childDomNode = domNode.FirstChild '获取第一个item节点
If nChildNodeList > 0 Then
For j = 1 To nChildNodeList '遍历每一个item节点
Set lastDomNode = childDomNode.FirstChild '获取item下面的第一个节点key
If Not lastDomNode Is Nothing Then '第一个元素(key)不为空
If Not lastDomNode.IsNull Then '对象不为空
If lastDomNode.NodeType = 1 Then '检查是否包括#Text
If lastDomNode.NextSibling.HasChildNodes Then 'Value标签里面有值
retList(lastDomNode.FirstChild.NodeValue) = lastDomNode.NextSibling.FirstChild.NodeValue
Else
retList(lastDomNode.FirstChild.NodeValue) = ""
End If
Else
If Not lastDomNode.NextSibling Is Nothing Then
retList(lastDomNode.NodeValue) = lastDomNode.NextSibling.NodeValue
Else
retList(lastDomNode.NodeValue) = ""
End If
End If
End If
Set childDomNode = childDomNode.NextSibling '获取下一个item节点,不存在则退出循环
If childDomNode Is Nothing Then
Exit For
End If
End If
Next
End If
End If
Next
End If
Exit Sub
getListErr:
print "解析WebService返回的Xml为List列表函数(parseXmlToList)出错,错误行:"+Cstr(Erl())+" 错误类型:"+Error()
End Sub