Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 13th, 2003, 09:03 AM
Authorized User
 
Join Date: Nov 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default XPath Problem

How do I trap an error that can error due to Invalid Path specified in XPath. For example I am trying to look for attribute with some name say 'Test', which is not available.

 
Old November 13th, 2003, 09:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

are you talking about using selectSingleNode method in MSXML? You could do:
Set node = domDoc.selectSingleNode("<<your XPATH>>")
If node Is Nothing Then
   ' its not there
Else
   ' it is there

Or you could do:
If domDoc.selectNodes("<<your XPATH>>").length = 0 Then
  ' its not there
Else
  ' it is there

If you just try to get the text without checking whether the node/attribute exists you'll get error 91 so you could trap for this if you want:

hth - if not pls post some code so we can see what you're using
Phil
 
Old November 13th, 2003, 10:43 AM
Authorized User
 
Join Date: Nov 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Thanks for the response.
The problem I have is that, whenever I use "selectSingleNode". I might get the error "Object or With Block not Set". This happens when my XML does not match the XPath.
So, this code,
Set node = domDoc.selectSingleNode("<<your XPATH>>")
may give out an error before I proceed to
If node Is Nothing Then
   ' its not there
Else
   ' it is there

Thanks,
Babloo

 
Old November 13th, 2003, 12:15 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yes that error is error 91. But you shouldn't get that error unless you try to access some property of the node/attribute without first checking that it exists.

Example:
domDoc.selectSingleNode("<<your XPATH>>").text will generate that error if the node/attrib doesn't exist, but
domDoc.selectSingleNode("<<your XPATH>>") will not error, it will just return Nothing.
 
Old November 14th, 2003, 01:28 AM
Authorized User
 
Join Date: Nov 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Phil,

Thanks a lot for the help.
So, this works.
    Dim x1 As New MSXML.DOMDocument
    x1.loadXML ("<P><T a='1' n='ttt'/><T a='2' n='yyy'/></P>")
    MsgBox x1.selectSingleNode("//T[@a=4]") Is Nothing 'Returns False
    MsgBox x1.selectSingleNode("//T[@a=2]") Is Nothing 'Returns True

Wow.
Tanks again Phil.

Babloo.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Xpath problem deean XSLT 10 June 18th, 2008 11:23 AM
Problem with xpath function Phanikumar Biztalk 1 May 22nd, 2008 02:32 AM
XPath problem meps XSLT 2 December 21st, 2005 06:14 AM
xpath syntax problem nrane26 XSLT 1 January 22nd, 2005 01:25 PM
XPath problem alexiev_nikolay C# 0 February 6th, 2004 03:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.