IsXML() really bulletproof?

Just starting out? Need help? Post your questions and find answers here.
User avatar
langinagel
Enthusiast
Enthusiast
Posts: 131
Joined: Fri Jan 28, 2005 11:53 pm
Location: Germany
Contact:

IsXML() really bulletproof?

Post by langinagel »

Hi,

I checked the forum and found no entry for IsXML().
I have this code:

Code: Select all

1      If  LoadXML(14,FileName)
2        If IsXML(14) <> 0 
3          sCheck = GetXMLNodeName(MainXMLNode(14))
and tried to see if IsXML() is able to tell a definitely not-XML-file (renamed PB-code file) from a real XML-file. This code breaks at line 3 after loading this PB-code file and the same IF-clause works fine while saving a XML-file.
PB 5.70x64 on Lubuntu 18.04

Update:
I updated <> 0 with = 0. Now neither non-XML nor correct XML-files pass this IF-clause.
Still confused.


Any hints?
Greetings
LN
https://www.doerpsoft.org

Boost. Work. Efficiency.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: IsXML() really bulletproof?

Post by Demivec »

IsXML() is doing what it should do. It shows whether there is an XML object associated with the ID#.

You should be using some other steps to check for a valid XML.

Code: Select all

Procedure.s getXMLStatusString(StatusCode)
  Protected Result$
  Select StatusCode
    Case  #PB_XML_Success                   : Result$ = "no error"
    Case  #PB_XML_NoMemory                  : Result$ = "out of memory"
    Case  #PB_XML_Syntax                    : Result$ = "syntax error"
    Case  #PB_XML_NoElements                : Result$ = "no element found"
    Case  #PB_XML_InvalidToken              : Result$ = "not well-formed (invalid token)"
    Case  #PB_XML_UnclosedToken             : Result$ = "unclosed token"
    Case  #PB_XML_PartialCharacter          : Result$ = "partial character"
    Case  #PB_XML_TagMismatch               : Result$ = "mismatched tag"
    Case  #PB_XML_DublicateAttribute        : Result$ = "duplicate attribute"
    Case  #PB_XML_JunkAfterDocElement       : Result$ = "junk after document element"
    Case  #PB_XML_ParamEntityRef            : Result$ = "illegal parameter entity reference"
    Case  #PB_XML_UndefinedEntity           : Result$ = "undefined entity"
    Case  #PB_XML_RecursiveEntityRef        : Result$ = "recursive entity reference"
    Case  #PB_XML_AsyncEntity               : Result$ = "asynchronous entity"
    Case  #PB_XML_BadCharacterRef           : Result$ = "reference to invalid character number"
    Case  #PB_XML_BinaryEntityRef           : Result$ = "reference to binary entity"
    Case  #PB_XML_AttributeExternalEntityRef: Result$ = "reference to external entity in attribute"
    Case  #PB_XML_MisplacedXML              : Result$ = "XML or text declaration not at start of entity"
    Case  #PB_XML_UnknownEncoding           : Result$ = "unknown encoding"
    Case  #PB_XML_IncorrectEncoding         : Result$ = "encoding specified in XML declaration is incorrect"
    Case  #PB_XML_UnclosedCDataSection      : Result$ = "unclosed CDATA section"
    Case  #PB_XML_ExternalEntityHandling    : Result$ = "error in processing external entity reference"
    Case  #PB_XML_NotStandalone             : Result$ = "document is not standalone"
    Case  #PB_XML_UnexpectedState           : Result$ = "unexpected parser state"
    Case  #PB_XML_EntityDeclaredInPE        : Result$ = "entity declared in parameter entity"
    Case  #PB_XML_FeatureRequiresDTD        : Result$ = "requested feature requires XML_DTD support in Expat"
    Case  #PB_XML_CantChangeFeatures        : Result$ = "cannot change setting once parsing has begun"
    Case  #PB_XML_UnboundPrefix             : Result$ = "unbound prefix"
    Case  #PB_XML_UndeclaringPrefix         : Result$ = "must not undeclare prefix"
    Case  #PB_XML_IncompletePE              : Result$ = "incomplete markup in parameter entity"
    Case  #PB_XML_XMLDeclaration            : Result$ = "XML declaration not well-formed"
    Case  #PB_XML_TextDeclaration           : Result$ = "text declaration not well-formed"
    Case  #PB_XML_PublicID                  : Result$ = "illegal character(s) in public id"
    Case  #PB_XML_Suspended                 : Result$ = "parser suspended"
    Case  #PB_XML_NotSuspended              : Result$ = "parser not suspended"
    Case  #PB_XML_Aborted                   : Result$ = "parsing aborted"
    Case  #PB_XML_Finished                  : Result$ = "parsing finished"
    Case  #PB_XML_SuspendedPE               : Result$ = "cannot suspend in external parameter entity"
    Case  #PB_XML_ReservedPrefixXML         : Result$ = "reserved prefix (xml) must not be undeclared or bound to another namespace name"
    Case  #PB_XML_ReservedPrefixXMLNS       : Result$ = "reserved prefix (xmlns) must not be declared or undeclared"
    Case  #PB_XML_ReservedNamespaceURI      : Result$ = "prefix must not be bound to one of the reserved namespace names"
  EndSelect
  ProcedureReturn Result$
EndProcedure

FileName.s = "example.xml" 

If  LoadXML(14, FileName)
  Debug "loaded"
  StatusCode = XMLStatus(14)
  Debug getXMLStatusString(StatusCode)
  If IsXML(14) <> 0 And StatusCode = #PB_XML_Success
    Debug IsXML(14)
    sCheck.s = GetXMLNodeName(MainXMLNode(14))
    Debug sCheck
  EndIf
EndIf

User avatar
langinagel
Enthusiast
Enthusiast
Posts: 131
Joined: Fri Jan 28, 2005 11:53 pm
Location: Germany
Contact:

Re: IsXML() really bulletproof?

Post by langinagel »

OK, problem solved and thread ready to be transfered to Coding questions.

My misunderstanding: a "valid and correct initialised" XML-Structure (in the memory!) can still have one or more listed flaws above.

Thanks Demivec !!!
https://www.doerpsoft.org

Boost. Work. Efficiency.
Fred
Administrator
Administrator
Posts: 18247
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: IsXML() really bulletproof?

Post by Fred »

If LoadXML() result is not zero, IsXML() will always returns true, so the double check is not needed. IsXML() only check if the object has been correctly created and is an XML.
Post Reply