Programming Guide : 4. Working with Classes : XML Support for OpenROAD : Examples of XML Capabilities
 
Share this page                  
Examples of XML Capabilities
The following examples demonstrate how to use some of the XML capabilities of OpenROAD. For more examples including source code, join the Actian Community at https://communities.actian.com/.
For more information about the system classes and constants used in these examples, see the Language Reference Guide and System Reference Summary.
Example: Writing XML
The following portion of 4GL code creates an XML document “my.xml” with elements “A” and “B” using the classes XMLDocument and XMLElement.
DECLARE
    xd      = XMLDocument;
    root    = XMLElement;
ENDDECLARE
{
  root.Name = 'A';
  root.AddAttribute(name='attr1', value='50');
  root.AddChildElement(name='B',valuetext='Value of B');
  xd.RootElement = root;
  xd.WriteToFile(filename='c:\temp\my.xml', indent=TRUE,
            standalone=TRUE, encoding=XE_UTF8);
}
This code produces the following XML output in the file “my.xml”:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<A attr1='50'>
    <B>Value of B</B>
</A>
Example: Writing XML Using Low-level Methods
The following portion of 4GL code creates the same XML document as in the previous example using the low-level methods of the class XMLDocument.
DECLARE
    xd  = XMLDocument;
    xa  = ARRAY OF XMLAttribute;
    str = StringObject;
ENDDECLARE
{
  xd.WriteStartDocument(filename='c:\temp\my.xml', indent=TRUE,
            standalone=TRUE, encoding=XE_UTF8);
  xa[1].Name='attr1';
  xa[1].Value='50';
  xd.WriteStartElement(name = 'A', attributes=xa);
  xd.WriteStartElement(name = 'B');
  str.Value = 'Value of B';
  xd.WriteTextData(string=str);
  xd.WriteEndDocument();
}
Example: Parsing XML
The following portion of code parses the XML file “my.xml” and, if an error is generated, displays the error text or, in case of success, identifies the name of the root element.
DECLARE
    xd      = XMLDocument;
ENDDECLARE
{
  IF xd.ParseURL(urllocation='C:\temp\my.xml')<>ER_OK
  THEN
      MESSAGE xd.Errortext;
  ELSE
      MESSAGE 'RootElement: '+xd.RootElement.Name;
  ENDIF;
}
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;
}