• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

Determining if an XPATH is valid

Hello,
I have an XML document which contains various paths (I use xpath to retrieve the values). What I need to be able to do is detect if the path I am looking for is valid. Can someone please tell me how to do this?

Worth 500 points.

Thanks,
Rick
0
richardsimnett
Asked:
richardsimnett
  • 4
  • 2
1 Solution
 
CEHJCommented:
May one ask why?
0
 
richardsimnettAuthor Commented:
I have a function which needs to determine if a configuration option is set or not prior to retrieving the value for the XPATH. If the option is not in the XPATH, I want to return a default value, if it is then just retrieve the value as normal. So I was thinking about adding a function called CheckXPATH(String path) which returns a boolean for whether it exists or not. I just cant seem to figure out how to determine if it exists.

Thanks,
Rick
0
 
CEHJCommented:
I don't understand that really. Perhaps you can explain in different words? In the meantime, i'll take a guess: all you can do is to search the DOM with the XPath and then return the default value if it doesn't match
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Mayank SAssociate Director - Product EngineeringCommented:
Depending on the API you are using, either the Xpath should return null or throw an exception - in which case you will know that it is not valid.
0
 
CEHJCommented:
>>throw an exception

I think that's unlikely
0
 
richardsimnettAuthor Commented:
CEHJ,
Actually in java 1.5.0-b06 it does throw an exception when the path cannot be found. So capturing that and having the function return null, and testing for the null result after the call allows me to return the default value as needed.

Thanks,
Rick
0
 
CEHJCommented:
>>when the path cannot be found

Really - meaning that the path is valid but couldn't be matched or meaning that the path is invalid?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now