PxPlus User Forum

Twitter Twitter Twitter

Author Topic: prevent error 65 in *obj/xml  (Read 1805 times)

Thomas Bock

  • Diamond Member
  • *****
  • Posts: 177
    • View Profile
prevent error 65 in *obj/xml
« on: August 18, 2020, 09:55:41 AM »
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

  • Diamond Member
  • *****
  • Posts: 3810
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: prevent error 65 in *obj/xml
« Reply #1 on: August 18, 2020, 01:52:27 PM »
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

  • Member
  • **
  • Posts: 13
    • View Profile
    • Earnest & Associates
Re: prevent error 65 in *obj/xml
« Reply #2 on: August 20, 2020, 12:02:31 PM »
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

  • Diamond Member
  • *****
  • Posts: 3810
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: prevent error 65 in *obj/xml
« Reply #3 on: August 20, 2020, 04:09:20 PM »
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

  • Member
  • **
  • Posts: 13
    • View Profile
    • Earnest & Associates
Re: prevent error 65 in *obj/xml
« Reply #4 on: August 20, 2020, 07:46:43 PM »
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!!