-
Quick way to check if an element exists?
I'm reading through all my xml elements. The structure is consistent between
all the children so it is easy to just go read in anything. In some cases
I add an additional element. If the element exists, I want to do something
different. The code looks like this:
Set node = item.selectSingleNode("description")
Then I use node.Text in some other functions. However, I do not see an easy
way to see setting the node has failed. Basically I can think of 3 options:
1) Make the structure consistent in all children, then check if the text
is "" or contains significant data.
2) Add error handling to blast through any problems and call different functions
if an error occurs, which indicates no element. I don't like this option
at all - kind of a major hack that leads to sloppy coding.
3) Create a function that goes through all children checking for a particular
element. I guess this would be the best option, but is this built into the
msxml parser? It seems like I shouldn't have to write this myself, that it
should be part of the parser and maybe I just don't see it.
Any thoughts?
-
Re: Quick way to check if an element exists?
Set node = item.selectSingleNode("description")
If Not node is Nothing Then
'// code here
Else
'// error stuff here
End If
"J Andree" <jandree@kpmg.com> wrote in message
news:39e48598$1@news.devx.com...
>
> I'm reading through all my xml elements. The structure is consistent
between
> all the children so it is easy to just go read in anything. In some cases
> I add an additional element. If the element exists, I want to do something
> different. The code looks like this:
>
> Set node = item.selectSingleNode("description")
>
> Then I use node.Text in some other functions. However, I do not see an
easy
> way to see setting the node has failed. Basically I can think of 3
options:
>
> 1) Make the structure consistent in all children, then check if the text
> is "" or contains significant data.
>
> 2) Add error handling to blast through any problems and call different
functions
> if an error occurs, which indicates no element. I don't like this option
> at all - kind of a major hack that leads to sloppy coding.
>
> 3) Create a function that goes through all children checking for a
particular
> element. I guess this would be the best option, but is this built into the
> msxml parser? It seems like I shouldn't have to write this myself, that it
> should be part of the parser and maybe I just don't see it.
>
> Any thoughts?
-
Re: Quick way to check if an element exists?
Regarding #3... There are simple XSL patterns that will do this for you.
To check all children:
oFoundNode = oNode.selectSingleNode("element_name")
To check all descendents (children of children, etc. all the way down the
tree):
oFoundNode = oNode.selectSingleNode(".//element_name")
If oFoundNode is nothing then 'that element wasn't there
Or use .selectNodes to return all nodes matching these criteria (instead
of just the first one) in a NodeList. You can then iterate through them
with NodeList.nextNode()...
"J Andree" <jandree@kpmg.com> wrote:
<snip>
>3) Create a function that goes through all children checking for a particular
>element. I guess this would be the best option, but is this built into the
>msxml parser? It seems like I shouldn't have to write this myself, that
it
>should be part of the parser and maybe I just don't see it.
>
>Any thoughts?
-
Re: Quick way to check if an element exists?
How about:
Set nodes = item.getElementsbyTagName("description")
'this returns a nodelistobject , which has a length property
If nodes.length > 0 Then
'Do one thing
Else
'Do another
End If
-Dave
"J Andree" <jandree@kpmg.com> wrote:
>
>I'm reading through all my xml elements. The structure is consistent between
>all the children so it is easy to just go read in anything. In some cases
>I add an additional element. If the element exists, I want to do something
>different. The code looks like this:
>
>Set node = item.selectSingleNode("description")
>
>Then I use node.Text in some other functions. However, I do not see an easy
>way to see setting the node has failed. Basically I can think of 3 options:
>
>1) Make the structure consistent in all children, then check if the text
>is "" or contains significant data.
>
>2) Add error handling to blast through any problems and call different functions
>if an error occurs, which indicates no element. I don't like this option
>at all - kind of a major hack that leads to sloppy coding.
>
>3) Create a function that goes through all children checking for a particular
>element. I guess this would be the best option, but is this built into the
>msxml parser? It seems like I shouldn't have to write this myself, that
it
>should be part of the parser and maybe I just don't see it.
>
>Any thoughts?
-
Re: Quick way to check if an element exists?
The easiest way to check for the existence of a node is to set the node like
you normally did and then use
if XMLVariable Is Nothing
And if the element exists it won't execute the code underlying, otherwise
it will.
I hope it helps
Michael
"J Andree" <jandree@kpmg.com> wrote:
>
>I'm reading through all my xml elements. The structure is consistent between
>all the children so it is easy to just go read in anything. In some cases
>I add an additional element. If the element exists, I want to do something
>different. The code looks like this:
>
>Set node = item.selectSingleNode("description")
>
>Then I use node.Text in some other functions. However, I do not see an easy
>way to see setting the node has failed. Basically I can think of 3 options:
>
>1) Make the structure consistent in all children, then check if the text
>is "" or contains significant data.
>
>2) Add error handling to blast through any problems and call different functions
>if an error occurs, which indicates no element. I don't like this option
>at all - kind of a major hack that leads to sloppy coding.
>
>3) Create a function that goes through all children checking for a particular
>element. I guess this would be the best option, but is this built into the
>msxml parser? It seems like I shouldn't have to write this myself, that
it
>should be part of the parser and maybe I just don't see it.
>
>Any thoughts?
-
Re: Quick way to check if an element exists?
"J Andree" <jandree@kpmg.com> wrote:
>
>I'm reading through all my xml elements. The structure is consistent between
>all the children so it is easy to just go read in anything. In some cases
>I add an additional element. If the element exists, I want to do something
>different. The code looks like this:
>
>Set node = item.selectSingleNode("description")
>
>Then I use node.Text in some other functions. However, I do not see an easy
>way to see setting the node has failed. Basically I can think of 3 options:
>
>1) Make the structure consistent in all children, then check if the text
>is "" or contains significant data.
>
>2) Add error handling to blast through any problems and call different functions
>if an error occurs, which indicates no element. I don't like this option
>at all - kind of a major hack that leads to sloppy coding.
>
>3) Create a function that goes through all children checking for a particular
>element. I guess this would be the best option, but is this built into the
>msxml parser? It seems like I shouldn't have to write this myself, that
it
>should be part of the parser and maybe I just don't see it.
>
>Any thoughts?
I'm thinking if you just want to output a specific tagset dependent on if
an element is present or not then you should be able to use xsl:if
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|