View Full Version : How to handle an empty element
slyfox2099
04-30-2004, 08:46 PM
Hello...
I have this problem with an empty xml element. I try to check to see if it exists, but when i try to check the [object].text property, it errors out. anyone know how to verify the element is empty so i can avoid the error?
Example code below:
set var = [object].documentElement.selectSingleNode("[node schema]")
newVar = [object].text *this is where the error occurs if the element is empty*
thanks in advance.
Alex Vincent
04-30-2004, 10:21 PM
newVar = oldVar.childNodes.length ? oldVar.text : null;
What does this do? The childNodes.length property (if 0) will mean it has no child nodes, and thus no text. It's empty, so newVar is set to false.
If childNodes.length > 0, you get oldVar.text.
By the way, the var word is reserved in JS, as it declares a variable. There's also no "set" word.
Is this JS?
slyfox2099
04-30-2004, 10:33 PM
This is in vbscript.
I am accessing only one element that is optional. It may actually not be present or present and just empty. when I try accessing the obj.TEXT property, it takes a dump and says the object is required.
This is in vbscript.In which case Alex's code should be altered:newVar = oldVar.childNodes.length ? oldVar.text : null;
to
if oldVar.childNodes.length > 0 then
newVar = oldVar.text
else
newvar = nothing
end ifThat may or may not work, I haven't programmed in basic for a long time.
M@rco
05-03-2004, 10:11 PM
In which case Alex's code should be altered:newVar = oldVar.childNodes.length ? oldVar.text : null;
to
if oldVar.childNodes.length > 0 then
newVar = oldVar.text
else
newvar = nothing
end ifThat may or may not work, I haven't programmed in basic for a long time.Close. Nothing is handled like an object (it is really a null pointer)... what is needed is an empty string (assuming that *is* what is wanted in this situation):If oldVar.ChildNodes.Length > 0 Then
newVar = oldVar.Text
Else
newvar = ""
End IfHowever, I'm surprised that it's actually throwing an error... what version of the Microsoft XML parser are you using?!
blefler
05-04-2004, 09:56 PM
Here's an alternate method that I use. And yes, vb always throws an error when you try to execute a method on an object that doesn't exist.
function getParam(xmlDoc, xpath)
set node = xmlDoc.selectSingleNode(xpath)
if not node is nothing then
getParam = node.text
else
getParam = ""
end if
end function
M@rco
05-08-2004, 02:57 AM
Here's an alternate method that I use. And yes, vb always throws an error when you try to execute a method on an object that doesn't exist.
function getParam(xmlDoc, xpath)
set node = xmlDoc.selectSingleNode(xpath)
if not node is nothing then
getParam = node.text
else
getParam = ""
end if
end functionThat's also fine, but it does involve creating an instance of a Node object (and for consistency I would suggest that should also be destroyed explicitly, but for a different point of view read this (http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx)).
EDIT: Corrected typos
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.