Page 1 of 1

XMLNodeFromPath fails when XMLNodePath inserts []'s

Posted: Fri Aug 22, 2025 2:25 pm
by dougmo52usr
I'm parsing a glade xml file. XMLNodeFromPath can't seem to handle XMLNodePath output when path has been abbreviated using "[]'s".

GtkButton @ /interface/object/child/object/child/object/child/object
GtkButton @ /interface/object/child/object/child/object/child/object

GtkToggleButton @ /interface/object/child/object/child/object/c[2]/object
XMLNodeFromPath(RootXMLNode(hXml),/interface/object/child/object/child/object/c[2]/object) returned 0

I have to StringReplace "c[" with "child[" before the abbreviated path works. Am I missing a flag in XMLNodePath which suppresses replacing "child[" with "c["?

Code: Select all

Procedure EnumerateNodes(hNode)
  ; if node exists and is an object node
  If hNode 
    If GetXMLNodeName(hNode) = #object$
      Protected thisXMLObjectNodePath$ = XMLNodePath(hNode)
      ; not required, but want to see if "child[1]" works
      thisXMLObjectNodePath$ = ReplaceString(thisXMLObjectNodePath$,"child","c[1]")
      Debug thisXMLObjectNodePath$
      ; replace abbreviated "c[" with "child[" since XMLNodeFromPath can't seem to handle a "c["
      thisXMLObjectNodePath$ = ReplaceString(thisXMLObjectNodePath$,"c[","child[")  
      Debug thisXMLObjectNodePath$
      ; try path where "c[" was expanded to "child["
      Protected nodeFromPath = XMLNodeFromPath(RootXMLNode(hXml),thisXMLObjectNodePath$)
      If nodeFromPath 
        Debug LSet(GetXMLAttribute(nodeFromPath,#class$),16) + " @ " + thisXMLObjectNodePath$
      Else
        Debug "?" + edit
      EndIf
    EndIf
    ; traverse children
    EnumerateNodes(ChildXMLNode(hNode))
    ; traverse siblings
    EnumerateNodes(NextXMLNode(hNode))
  EndIf
EndProcedure
produces the output:

Code: Select all

/interface/object
/interface/object
GtkWindow        @ /interface/object
/interface/object/c[1]/object
/interface/object/child[1]/object
GtkFixed         @ /interface/object/child[1]/object
/interface/object/c[1]/object/c[1]/object
/interface/object/child[1]/object/child[1]/object
GtkBox           @ /interface/object/child[1]/object/child[1]/object
/interface/object/c[1]/object/c[1]/object/c[1]/object
/interface/object/child[1]/object/child[1]/object/child[1]/object
GtkButton        @ /interface/object/child[1]/object/child[1]/object/child[1]/object
/interface/object/c[1]/object/c[1]/object/c[2]/object
/interface/object/child[1]/object/child[1]/object/child[2]/object
GtkToggleButton  @ /interface/object/child[1]/object/child[1]/object/child[2]/object
/interface/object/c[1]/object/c[2]/object
/interface/object/child[1]/object/child[2]/object
GtkBox           @ /interface/object/child[1]/object/child[2]/object
/interface/object/c[1]/object/c[2]/object/c[1]/object
/interface/object/child[1]/object/child[2]/object/child[1]/object
GtkSwitch        @ /interface/object/child[1]/object/child[2]/object/child[1]/object
/interface/object/c[1]/object/c[2]/object/c[2]/object
/interface/object/child[1]/object/child[2]/object/child[2]/object
GtkEntry         @ /interface/object/child[1]/object/child[2]/object/child[2]/object