Avoid NIL on XMLNodeList?

Hi team.

I have a question to do, How can I avoid a NilObjectException on a XMLNodeList?

I have a big code elaborated, And I make a query to the XMLfile with this:

Dim nodes, nodes2,nodes3,nodes4,nodes5 As XmlNodeList
nodes = xml.Xql("//cfdi:Conceptos/cfdi:Concepto")

One kind of XML has this //cfdi:Conceptos/cfdi:Concepto".

When It has this there’s no problem, the issue becames when the XML file doesn’t have this.

I tried to avoid it using pragma BreakOnExceptions off but without succeess

Any Ideas?, What AM I wrong?

In synthesis i wanna know if theres a way to analyze with an “IF” the xmlNodelist If the path described exists then Do the rest of the code.

If doesn’t exists, do nothing.

Have a look at Try…Catch in the Language Ref.
Basically your try block would be the

nodes = xml.Xql("//cfdi:Conceptos/cfdi:Concepto")

and you would catch

If nodes = Nil Then

and handle it however is appropriate for your code.

And you really don’t need try/catch. If xql doesn’t find a match, it returns nil. Test for nil before you try to use the result.

if nodes <> nil then

I’m trying with both but I don’t Got a NILObjectException, I got a XMLException, and that is the explanation in the compiler:

Error Number: 24
Line: Line:1
Message: msg:Invalid namespace prefix ‘//cfdi:Conceptos/cfdi:Concepto’
Node: node.element ‘cfdi:Comprobante
reason: msg:invalid namespace prefix ‘nomina:Percepcion’

You said NilObjectException earlier. Try supplying the optional parameter to XQL().

I found Where is the child what am I looking for into the XML File:


I also tried with:
call xml.FirstChild.Child(4).Child(0).Child(0).name
Catch err As NilObjectException

End Try

In order to do this: If this child exists then do the code, else do nothing.

Is there a practical way to do it, having no errors?

I found a way to Know if the Child exists:

If xml.FirstChild.Child(4).Child(0).Child(0) <>Nil Then
MsgBox “Existing Child”
Msgbox “Child not exist”
End If

Hi Gerardo,

Glad to see that there is a solution.

There is a book on this topic at Great-White-Software which might help.

Have a good weekend!