XML request from ASP without XSL
I have a very simple XML:
<properties>
<property>
<name>BobFish</name>
<explaination>Bob is a fish.</explaination>
</property>
<property>
<name>DaveFish</name>
<explaination>Dave is a fish.</explaination>
</property>
I want to query if from ASP. Sort of:
Response.Write (GetExplaination("BobFish"))
This is the function I have so far:
Function GetExplaination(strXMLFile, strName)
'Declare local variables
Dim objXML
Dim objNode
set objXML = Server.CreateObject("Microsoft.XMLDOM")
objXML.load(strXMLFile)
Set objNode = objXML.SelectSingleNode("properties/property[name='" & strName & "']")
GetExplaination = "Nothing Yet"
End Function
So, I have an objNode containing the data I need. How do I retrieve an Explanation from this field?
+1
source to share
2 answers
Function GetExplaination(strXMLFile, strName)
Dim objXML
Dim objNode
Dim strXPath
Set objXML = CreateObject("Microsoft.XMLDOM")
objXML.load(strXMLFile)
''// enclose in single- or double quotes accordingly
If InStr(strName, "'") > 0 And InStr(strName, """") = 0 Then
strName = """" & strName & """"
ElseIf InStr(strName, "'") = 0 And InStr(strName, """") > 0 Then
strName = "'" & strName & "'"
Else
''// both single and double quotes in a string are unsupported
strName = "''"
End If
strXPath = "/properties/property[name = " & strName & "]/explaination"
Set objNode = objXML.SelectSingleNode(strXPath)
If Not objNode Is Nothing Then
GetExplaination = objNode.Text
End If
End Function
...
Response.Write GetExplaination("Fish.xml", "DaveFish")
I would not recommend loading the document every time you are looking for a single property. Load and parse the document ahead and pass the document reference to the function instead of the file path.
+2
source to share