prevent error 65 in *obj/xml

Started by Thomas Bock, August 18, 2020, 09:55:41 AM

Previous topic - Next topic

Thomas Bock

It is very laborious to ask for the existance of a node, if the logic must be able to deal with the missing value:
xml'find_node("missing/node")'value$

As mid($$,4,5) doesn't throw an error 47, I would prefer to get an empty string in this case, too.

Mike King

The simplest solution is to enclose the request in a TRY function as in:

X$ = TRY( xml'find_node("missing/node")'value$ , "")

You can actually then define any default value you want to return when the node access fails.
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

Josh Fake

Thomas,

I actually wrote a function  to trap the error and reduce coding inside of program.

14800 ! def function
14805 def fnxget$(id,tag$)
14810 local x$
14820 x$=evs("id'find_node("+quo+tag$+quo+")'value$",err=*next)
14825 return x$
14830 end def

Then utilize as:
x$=fnxget$(xml,"missing/node")

Hope this helps!!

Mike King

Josh

From a speed perspective the TRY would be better -- so how about a compromise?

def fnxget$(local id, local tag$)=TRY( id'find_node(tag$), "")

Or if you would rather create new XML object as follows:

DEF CLASS "MyXml"
LIKE "*obj/xml"
FUNCTION Find_Value$(tag$)=TRY(_obj'Find_Node(tag$)'value$,"")
END DEF

Now just use this object instead of *obj/xml and use the method Find_Value$() and as it inherits all the other methods from *obj/xml there will be now changes to your other code.



Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

Josh Fake

Mike,

I think I wrote this function before the try() system function was available back in early 2010 or 2011.  I was also using the MsXML2.ServerXMLHttp object which was significantly more code to retrieve a single value from a node/element.  i.e x$=evs("id'getelementsbytagname("+quo+tag$+quo+")'item(0)'text$",err=*next). So when migrating to the *obj/xml parser I just kept most of the syntax the same.

But I will definitely utilize your suggestion keeping speed in mind when I am tweaking those document import interfaces again. It will definitely help!

Thanks Mike!!