Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML DOM - How do i check if tag exists? 2

Status
Not open for further replies.

ftpdoo

Programmer
Aug 9, 2001
202
0
0
GB

I am doing the following and my code falls over at the last line because the tag doesn't exist! Which sometimes may be the case.. Is there any way which i can a check?

Dim Dom As Object
Set Dom = CreateObject("MSXML.DomDocument")
Dom.async = False
Dom.Load gsBASE_PATH & "\" & gsENV_PATH & "\" & gsDATA_PATH & "\System.xml"
gsPCPhotoDir = Dom.selectSingleNode("//SystemParams/Params/PCPHOTODIR").Text

I want to do something like follows but not sure of the syntex:

if TAGEXISTS then
gsPCPhotoDir = Dom.selectSingleNode("//SystemParams/Params/PCPHOTODIR").Text
end if

Any help would be MUCH appricated!
Jonny
 
Why not step through the nodes in the file, comparing each one to check if it is the node you want. And if it is not found, inform the user after the loop finished by using a flag.
 
I had thought about that but because of the size of this file i would prefare not too.. Without looping through the file is there not a more staightford way to find if this tag exists?

:O)
 
You could also use 'On Error GoTo ...' to catch the exception and do some clean-up if the node does not exist.
 

How would i loop around to check if this node exists?
 
Why not just do this?

Code:
    Dim Dom As Object
    Set Dom = CreateObject("MSXML.DomDocument")
    Dom.async = False
    Dom.Load gsBASE_PATH & "\" & gsENV_PATH & "\" & gsDATA_PATH & "\System.xml"
    Set gsPCPhotoDir = Dom.selectSingleNode("//SystemParams/Params/PCPHOTODIR")

   if Not gsPCPhotoDir Is Nothing then
      gsPCPhotoDirText = gsPCPhotoDir.Text
   end if

Spong
 

The problem is i cant even get to that line - getting the error:

Run-time error '91':

Object variable or With block Variable not set

Because the tag does not exist. This is the line that causes the error:

gsPCPhotoDir = Dom.selectSingleNode("//SystemParams/Params/PCPHOTODIR").Text


Thanks!
 
Sorry Spong - your solution worked great! Your a STAR and i'll assign you a star now! :O)

Jonny
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top