Tag Archives: C71

XML with Clarion 7

As with any other file format there are two basic processes that can be done with XML files; Reading and Writing.
C7 supports doing both in many ways, without the need of any additional tools.
In this article we’ll focus on two different methods, one for writing and one for reading.

The writing.

The method discussed for writing is not new to C7 but it was never used in this way.
I’m talking about the XMLGenerator class, the same class as used in the Report Output templates.
Most of the classes used in the Report Output were designed to be used independent of the Report itself.
The XMLGenerator is no exception, and that is why it can be used in a very simple way to create XML files by hand coding the calls to the XMLGenerator methods.

The class has methods to create an in memory XML document and once that is done we can flush the
content to a file.

The methods are self-documented in the functionality, but lets introduce some code.

INCLUDE('ABPRXML.INC'),ONCE
XML XMLGenerator
  CODE
XML.Init('breakfast_menu.XML')
XML.OpenDocument()
XML.SetEncoding('ISO-8859-1')
XML.SetRootTag('breakfast_menu')
XML.AddComment('The root tag is the first tag in the doc')
XML.AddTag('food','')
XML.AddAttribute('recordNumber','1','food')
XML.CloseDocument()

First, as with any class contained within a separate module we need to include the module.
the class is declared in ABPRXML.INC
After that we create a new class instance of type XMLGenerator that we conveniently call XML

After that we call the Init method passing the xml file name we want to create.

XML.Init(‘breakfast_menu.XML’)

Then we open the document calling the OpenDocument()
And start to create the document content.

XML.OpenDocument()

The class supports many formats for its XML. In this case we use a single root node
that we set by calling the SetRootTag method.

XML.SetRootTag(‘breakfast_menu’)

After that we need to start adding nodes to our tree.

XML.AddTag(‘food’,”)

The Tags can have both attributes and content.
In this case we add attributes to the last added Tag named ‘food’ with the AddAttribute method.

XML.AddAttribute(‘recordNumber’,’1′,’food’)

After adding all our Tags and Attributes we just need to close the document and the content will be written to disk.

XML.CloseDocument()

Some of the methods used to add content are:

SetRootTag           PROCEDURE(STRING pRootTag)
AddComment           PROCEDURE(STRING pComment)
AddTag               PROCEDURE(STRING pName,STRING pValue,BYTE pIsCData=0,<STRING pParent>)
SetTagValue          PROCEDURE(STRING pTagName,STRING pValue,BYTE pIsCData=0)
AddAttribute         PROCEDURE(STRING pName,STRING pValue,STRING pTagName)

Some of the methods to modify properties of the XML document are:

AddXMLHeaderAttribute   PROCEDURE(STRING pName,STRING pValue)
SetEncoding             PROCEDURE(STRING pEncoding)
SetXSL                  PROCEDURE(STRING pXSLFileName)
SetUseCRLF              PROCEDURE(BYTE pTrue=1)

As you can see, you can create any type of XML document, writing just a little code.
You don’t have to worry about the formatting or strings or opening closing Tags, or anything, just call
the methods and the class will create the document for you.

Just remember that the physical file does not exist until you close the document.

Attached in this document is the full example to generate the XML file.

In the next Part 2 of this article we will be discussing about reading this document using Clarion 7

NOTE: This class is part of the Report output Generator to XML and is included in C7 EE package and can be added to C7 PE

part1_xmlgenerator