Methods used for creating and saving XML documents

Once an XMLDOCUMENT variable is declared, an empty XML document is implicitly created for the variable. You can add nodes to this document and make other modifications and then save the document to the disk. If you want to work with an existing document, you can initialize the variable using the Load() method.

create()

This method creates a new empty object of a XMLDOCUMENT data type

DEFINE document XMLDOCUMENT

...

CALL document.Create("Contacts")

All the file names you use in your 4gl development must be platform-independent.

If you use platform-dependent names (e.g., combine upper and lower case, use colons, finish names with a full stop, etc.), please remember that they might cause different problems when you move your project to another platform.

load()

This method parses the specified XML file into the XMLDOCUMENT variable used with it. It accepts  two parameters: one of a character data type which should consist of the file name and path to it and a boolean parameter which specifies whether the new lines will be ignored. If the second parameter is set to TRUE, the new lines are ignored, otherwise they are treated as empty nodes. E.g.:

CALL xmldocument_var.Load("C:/xml_files/my.xml", TRUE)

Pay attention, that if the loaded file contains new line characters, other methods applied to the variable containing such a file may produce incorrect results, if the second parameter is set to FALSE. It is advisable that you use files without the new line symbols in this case.

When you specify a file name, be careful with its case.

UNIX is case-sensitive, so it will treat files which names come in different cases as separate files.

toString()

This method returns the referenced document in the form of a character string, it accepts no arguments. In this way you can assign the contents of XMLDOCUMENT variables to other 4GL variables.

LET str = xmldoc_var.ToString()

fromString()

This method assigns a character string passed to it as a parameter as a value to the referenced XMLDOCUMENT variable. It accepts two parameters. The first parameter is a character string containing the XML code and the second parameter is a Boolean value which defines whether the  white spaces contained in the character string, if any, should be ignored during converting it into a XMLDOCUMENT value. If the last argument is set to TRUE, the whitespaces will be ignored. Set this option to TRUE, if the character string contains whitespaces and new line symbols between the node tags, otherwise other methods used to count and retrieve nodes will return incorrect results, treating new line symbols as nodes.

CALL xmldoc_var.ToString(str, true)

If the character string contains any other nodes before the root node, e.g. a comment node or a DTD node, these nodes will be omitted and will not be assigned to the variable.

save()

To save the contents of an XMLDOCUMENT variable as an XML file the Save() method is used. It accepts a single parameter of a character data type which contains the name of the destination xml file and the optional path. If the file does not exist, it is created when this method is called.

Here is an example of an xml file created in a 4GL application and then saved to disk:

MAIN

  DEFINE document XMLDOCUMENT

  DEFINE node XMLNODE

  DEFINE comment_node XMLNODE

  DEFINE child_node XMLNODE

  

  CALL document.Create("root")

  LET node = document.GetFirstDocumentNode()

  #first some nodes and child nodes are created

  LET comment_node = document.CreateComment("Sample comment")

  LET child_node = document.CreateElement("child")

  CALL child_node.SetNodeValue("Some random text")

  # then they are all gathered together into a root node

  CALL node.AppendChild(comment_node)

  CALL node.AppendChild(child_node)

  

  # and the XMLDOCUMENT variable is saved into an XML file

  CALL document.Save("out.xml")

END MAIN

Here is the text of the XML file, out.xml, the program above generates:

<?xml version="1.0"?>

<root>

  <!--Sample comment-->

  <child>Some random text</child>

</root>

 

Contact Us

Privacy Policy

Copyright © 2025 Querix, (UK) Ltd.