Ezog
asked on
Using XPath to test if XML node exists
Using ASP, I'm trying to access specific XML nodes with .SelectSingleNode("//TAGNA ME") and test if the tag exists. Accessing the nodes (when they exist) is no problem, but the test for existance has me stumped.
I tried:
set thisNode = xmlDoc.documentElement.Sel ectSingleN ode("//TAG NAME")
If StrComp("thisNode.Text", "" )<> 0 Then
Response.Write thisNode.Text
End If
but it gives me an 'object required' error
Should I use getElementsByTagName() instead?
I'm sure this is not a very difficult question for you experts, but I'm stuck on this and a quick solution is very important, so I'm assigning more points.
Thanks
I tried:
set thisNode = xmlDoc.documentElement.Sel
If StrComp("thisNode.Text", "" )<> 0 Then
Response.Write thisNode.Text
End If
but it gives me an 'object required' error
Should I use getElementsByTagName() instead?
I'm sure this is not a very difficult question for you experts, but I'm stuck on this and a quick solution is very important, so I'm assigning more points.
Thanks
do not use "set" (and maybe """ around thisNode.Text)
and make sure you have
> Dim xmlDoc As New XML.XMLDocument
before you do anything
> Dim xmlDoc As New XML.XMLDocument
before you do anything
or
> Set xmlDoc=Server.CreateObject ("microsof t.xmldom")
> Set xmlDoc=Server.CreateObject
if none of these help try
> thisNode = xmlDoc.documentElement.Sel ectSingleN ode("//TAG NAME").Tex t
that's all I could think of.. :)
> thisNode = xmlDoc.documentElement.Sel
that's all I could think of.. :)
ASKER
I have:
set xmlDocL = CreateObject("Microsoft.XM LDom")
xmlDoc.async = False
Set xmlDoc = objXMLHTTP.responseXML
If I use
thisNode = xmlDoc.documentElement.Sel ectSingleN ode("//TAG NAME").Tex t
I still get the 'object reqiured' error
set xmlDocL = CreateObject("Microsoft.XM
xmlDoc.async = False
Set xmlDoc = objXMLHTTP.responseXML
If I use
thisNode = xmlDoc.documentElement.Sel
I still get the 'object reqiured' error
ASKER
when I tried:
set objElements = xmlDoc.getElementsByTagNam e("TAGNAME ")
If objElements.Item(0) Is Nothing Then
thisNode = ""
Else
thisNode = objElements.Item(0).nodeTy peString
End If
Response.Write thisNode
I get 'element' as response instead of the string value
set objElements = xmlDoc.getElementsByTagNam
If objElements.Item(0) Is Nothing Then
thisNode = ""
Else
thisNode = objElements.Item(0).nodeTy
End If
Response.Write thisNode
I get 'element' as response instead of the string value
what line is that you getting error on..?
> set xmlDocL = CreateObject("Microsoft.XM LDom")
what's xmlDoc-"L " shouldn't it be xmlDoc
what's xmlDoc-"L " shouldn't it be xmlDoc
ASKER
the "L" - just a typo,
I get the error on the Response.Write line
I get the error on the Response.Write line
ASKER
I also tried:
set thisNode = xmlDoc.documentElement.Sel ectSingleN ode("//TAG NAME")
If len (thisNode.Text) >0 Then
Response.Write thisNode.Text
End If
I get an error only when the tag does not exist.
set thisNode = xmlDoc.documentElement.Sel
If len (thisNode.Text) >0 Then
Response.Write thisNode.Text
End If
I get an error only when the tag does not exist.
try
> Set xmlDoc = CreateObject("Msxml2.DOMDo cument")
and something very important
> Set xmlDoc = objXMLHTTP.responseXML
after this xmlDoc is not an object anymore...
I think that correct way would be following...
Set xmlDoc = CreateObject("Msxml2.DOMDo cument")
xmlDoc.load(path_to_the_sa me_file_wh ich_is_loa ded_into_o bjXMLHTTP)
set thisNode = xmlDoc.documentElement.Sel ectSingleN ode("//TAG NAME")
If len (thisNode.Text) >0 Then
Response.Write thisNode.Text
End If
> Set xmlDoc = CreateObject("Msxml2.DOMDo
and something very important
> Set xmlDoc = objXMLHTTP.responseXML
after this xmlDoc is not an object anymore...
I think that correct way would be following...
Set xmlDoc = CreateObject("Msxml2.DOMDo
xmlDoc.load(path_to_the_sa
set thisNode = xmlDoc.documentElement.Sel
If len (thisNode.Text) >0 Then
Response.Write thisNode.Text
End If
when you try "thisNode.Text" it's looking for the object, but with "Set xmlDoc = objXMLHTTP.responseXML" "xmlDoc" would never be the object
ASKER
I inserted
Set xmlDoc = CreateObject("Msxml2.DOMDo cument")
and replaced
Set xmlDoc = objXMLHTTP.responseXML
with
xmlDoc.load("http://URL")
path_to_the_same_file_whic h_is_loade d_into_obj XMLHTTP
but get:
Microsoft VBScript runtime error '800a01a8'
Object required
/response.asp, line 49
which is:
set thisNode = xmlDoc.documentElement.Sel ectSingleN ode("//TAG NAME")
Also, I only received an error if the tag is missing. If the tag exists, I can enter its value into the form with the following:
Set rootNode = xmlDoc.documentElement
Set thisNode = rootNode.SelectSingleNode( "//TAGNAME ")
Response.Write thisNode.Text
Set xmlDoc = CreateObject("Msxml2.DOMDo
and replaced
Set xmlDoc = objXMLHTTP.responseXML
with
xmlDoc.load("http://URL")
path_to_the_same_file_whic
but get:
Microsoft VBScript runtime error '800a01a8'
Object required
/response.asp, line 49
which is:
set thisNode = xmlDoc.documentElement.Sel
Also, I only received an error if the tag is missing. If the tag exists, I can enter its value into the form with the following:
Set rootNode = xmlDoc.documentElement
Set thisNode = rootNode.SelectSingleNode(
Response.Write thisNode.Text
how about
Set xmlDoc = CreateObject("Msxml2.DOMDo cument")
xmlDoc.load(path_to_the_sa me_file_wh ich_is_loa ded_into_o bjXMLHTTP)
set xmlResponse=xmlDoc.respons eXML 'also try without "set"
thisNode = xmlResponse.documentElemen t.SelectSi ngleNode(" //TAGNAME" )
If len (thisNode.Text) >0 Then
Response.Write thisNode.Text
End If
Set xmlDoc = CreateObject("Msxml2.DOMDo
xmlDoc.load(path_to_the_sa
set xmlResponse=xmlDoc.respons
thisNode = xmlResponse.documentElemen
If len (thisNode.Text) >0 Then
Response.Write thisNode.Text
End If
ok, ignore my last post and try this first
Set xmlDoc = CreateObject("Msxml2.DOMDo cument")
xmlDoc.load(path_to_the_sa me_file_wh ich_is_loa ded_into_o bjXMLHTTP)
thisNode = xmlDoc.documentElement.Sel ectSingleN ode("//TAG NAME")
If len (thisNode.Text) >0 Then
Response.Write thisNode.Text
End If
is loading file XML or some other type..?
Set xmlDoc = CreateObject("Msxml2.DOMDo
xmlDoc.load(path_to_the_sa
thisNode = xmlDoc.documentElement.Sel
If len (thisNode.Text) >0 Then
Response.Write thisNode.Text
End If
is loading file XML or some other type..?
ASKER
I get with and without "set":
'Microsoft VBScript runtime error '800a01b6'
Object doesn't support this property or method: 'responseXML'
/response.asp, line 27
'Microsoft VBScript runtime error '800a01b6'
Object doesn't support this property or method: 'responseXML'
/response.asp, line 27
did you try one above your post..?
ASKER
I'm getting a feed with about 30 possible tags but only about half of them are returned at any given time. I don't want to use stylesheets so I try to find the missing tags with conditionals...
maybe I'm going about it the wrong way
maybe I'm going about it the wrong way
ASKER
hm, I don't understand that comment. What do you mean with "above your post"?
I mean this one
Set xmlDoc = CreateObject("Msxml2.DOMDo cument")
xmlDoc.load(path_to_the_sa me_file_wh ich_is_loa ded_into_o bjXMLHTTP)
thisNode = xmlDoc.documentElement.Sel ectSingleN ode("//TAG NAME")
If len (thisNode.Text) >0 Then
Response.Write thisNode.Text
End If
Set xmlDoc = CreateObject("Msxml2.DOMDo
xmlDoc.load(path_to_the_sa
thisNode = xmlDoc.documentElement.Sel
If len (thisNode.Text) >0 Then
Response.Write thisNode.Text
End If
ASKER
I think I might have figured it out:
This one works with the old
Set xmlDoc = objXMLHTTP.responseXML
set objElements = objXML.selectSingleNode("/ /ZONING")
if objElements is nothing then
Response.Write "(Not available)"
else
Response.Write objElements.Text
end if
This one works with the old
Set xmlDoc = objXMLHTTP.responseXML
set objElements = objXML.selectSingleNode("/
if objElements is nothing then
Response.Write "(Not available)"
else
Response.Write objElements.Text
end if
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
actualy, I rewrote the thing and used objXML instead of xmlDOC (just for reference or the code would not make sence!)
ASKER
I think it works... at least so far, but I would like to leave the treat open until I have done some more testing. Thank you so much for you help! I will award your points on closing.
> Using XPath to test if XML node exists
Is there any reason for not using simply SelectSingleNode("count(// TAGNAME)") Xpath? It should return the count of TAGNAME nodes, 0 - if no such nodes exist...
Is there any reason for not using simply SelectSingleNode("count(//
ASKER
I tried the SelectSingleNode("count(// TAGNAME)") and got the following error:
msxml3.dll error '80004005'
Unknown method. -->count(//<--STATE)
msxml3.dll error '80004005'
Unknown method. -->count(//<--STATE)