Simple XML Generation without the Report
Most of you should be aware of our Advanced Report Generation templates that were introduced in Clarion 6. There is Report to HTML, Report to PDF, Report to Text, and Report to XML.
Quite often we receive requests asking for a simple way to create XML documents, developers looking to programatically create XML docs without using the Report procedure.
One of our developers, Diego Borojovich, shows you here how easy this is to do using the XMLGenerator class.
PROGRAM
MAP
END
INCLUDE('ABPRXML.INC'),ONCE
XML XMLGenerator !create an instance of the XMLGenerator class
CODE
XML.Init('breakfast_menu.XML')
XML.OpenDocument()
XML.SetEncoding('ISO-8859-1')
XML.XMLVersion = '1.0'
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.AddTag('name','Belgian Waffles',false,'food')
XML.AddTag('price','$5.95',false,'food')
XML.AddTag('description','two of our famous Belgian Waffles with plenty of real maple syrup',false,'food')
XML.AddTag('calories','650',false,'food')
XML.AddTag('food','')
XML.AddAttribute('recordNumber','2','food')
XML.AddTag('name','Strawberry Belgian Waffles',false,'food')
XML.AddTag('price','$7.95',false,'food')
XML.AddTag('description','light Belgian waffles covered with strawberries and whipped cream',false,'food')
XML.AddTag('calories','900',false,'food')
XML.CloseDocument()
*****************************
Here are the xml elements created by the program:
<breakfast_menu>
<food recordNumber="1">
<name>gt;Belgian Waffles</name>
<price>$5.95</price>
<description>
two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food recordNumber="2">
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>
light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
</breakfast_menu>
That's all there is to it. We're working on adding
some additional documentation for this class, but in the mean time if
you examine the parameter names of each of the methods, the class is
nearly self-documenting.
We have a new XML reader class for C7 that will read any sized XML file very quickly and is super easy to use -- and is pure Clarion code. Stay tuned for more info, and now I'm going bowling!
SoftVelocity Inc.