4. Working with Classes : XML Support for OpenROAD : Examples of XML Capabilities : Example: Writing XML
 
Share this page                  
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();
}