tags:

views:

187

answers:

2

I have some 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. Something like:

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 objNode containing the data I need. How do I extract the "Explaination" field from this?

+2  A: 

First add a path to your explanation node to get that rather than the whole property node.

Set objNode = objXML.SelectSingleNode("properties/property[name='" & strName & "']/explanation")

Next return the innertext of the node to get the text you're after

GetExplaination = objNode.Text
Robin Day
my bad, thanks, fixed
Robin Day
+2  A: 
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 each time you look for a single property. Load and parse the document up-front and pass in the document reference to the function instead of the file path.

Tomalak
Thanks, much appreciated.
theaxe