Example: Viewing an XML File in a TreeviewField
The following portion of code populates a TreeView with XML elements and populates other fields with details of the XMLElement according to the selected tree node. The value of the Attributes TableField (variable name "attrs") is an array of XMLAttribute.
initialize()=
declare
tvf = TreeViewField default null;
t = Tree;
r = TreeNode DEFAULT NULL;
n = TreeNode DEFAULT NULL;
xd = XMLDocument;
xe = XMLElement DEFAULT NULL;
populate_tree = PROCEDURE;
populate_children = PROCEDURE;
reply = StringObject;
enddeclare
{
tvf = FIELD(xmltree);
t = tvf.Tree;
}
ON CLICK browse_btn=
{
reply.Value = '*.xml[XML Files]';
IF CurFrame.FilePopup(reply = reply) = PU_OK THEN
xmlfilename = reply.Value;
CALLPROC populate_tree(filename = xmlfilename);
ENDIF;
}
ON SETVALUE xmlfilename =
{
CALLPROC populate_tree(filename = xmlfilename);
}
ON SETVALUE xmltree =
{
xe = XmlElement(tvf.TriggerNode.ClientData);
xe.GetCharacterValue(string = BYREF(charval));
FIELD(charval).StringValue = charval; // Set the "Text Value" EntryField
attrs = xe.Attributes; // Fill "Attributes" TableField
}
PROCEDURE populate_tree(filename = VARCHAR(256) NOT NULL)=
{
IF t.RootNode IS NOT NULL THEN
t.DeleteNode(node = t.RootNode, allnodes = TRUE);
ENDIF;
IF xd.ParseURL(urllocation=filename)<>ER_OK
THEN
MESSAGE xd.Errortext;
ELSE
r = t.AddNode(name = xd.RootElement.Name);
r.ClientData = xd.RootElement;
CALLPROC populate_children(node=r, elem=xd.RootElement);
ENDIF;
}
PROCEDURE populate_children(node=TreeNode, elem=XMLElement)=
declare
n = TreeNode DEFAULT NULL;
xn = XmlNode DEFAULT NULL;
xe = XmlElement DEFAULT NULL;
i = integer not null default 1;
enddeclare
{
FOR i=1 TO elem.Children.LastRow DO
xn = elem.Children[i];
IF xn.IsA(class=XMLElement)=TRUE THEN
xe = XMLElement(xn);
n = t.AddNode(name = xe.Name, relative = node, relation = TN_LASTCHILD);
n.ClientData = xe;
CALLPROC populate_children(node=n, elem=xe);
ENDIF;
ENDFOR;
}