In Styx, if you select a non-existent node, it will crash. Sometimes you may have a situation where a child node may or may not exist, quite legitimately.
This function will solve this problem for you. If it exists, it will move to the child node and return the node index. If it doesn't exist, you will remain at the current node, and teh return value will be -1.
If you pass a name$, it will use that. If you don't, it will use the index number instead.
An example of use would be:
if XMLnodeToChild(xmlNo, "CHILD_ITEM", 0) <> -1
`Process the child item
XML Node to Parent
else
` The node didn't exist
endif
Function:
function XMLnodeToChild(xmlNo, child$, child)
` *******************************************************************
` *** XMLnodeToChild ***
` *** Moves to Child Node by name or by id if no name passed("") ***
` *** Returns node number if successful ***
` *** or -1 if unsuccessful ***
` *******************************************************************
` Count children
x = XML child node count(xmlNo)
` If no children, end
if x = 0
exitfunction -1
else
` If by index and index available, OK!
if child$ = "" and x > child
xml node to child xmlNo, child
exitfunction x
endif
` If by index and index not available, end
if child$ = "" and x <= child
exitfunction -1
endif
` Must be by name, see if available
xml node to child xmlNo,0
for n = 0 to x - 1
if child$ <> ""
if get xml node name(xmlNo) = child$
exitfunction n
endif
endif
next n
xml node to parent xmlNo
endif
endfunction -1