|
|
| > Expat4D Home > Documentation |
xmlObj is a module which was written for the Expat4D Demo Database to show how XML documents could be stored using ObjectTools from Automated Solutions Group. After looking at the way XML DOM functionality could be used Java and PHP, it was decided to make the implementation similar to these languages. 4D does not have an OOP language and implementing something XML DOM in 4D would normally be extremely difficult. Using ObjectTools, however, makes this a much easier task.
With xmlObj, you can parse an XML document into a group of objects referenced by a root object, then extract any information you need, or modify the document using the objects, then when finished, output the object as an XML document. You can also create new documents using a simple set of xmlObj routines.
To convert an existing document to an xmlObj, the component requires the Expat4D plug-in to parse the document. However, if you only require to create new documents, the Expat4D plug-in is not required.
With more and more applications using XML as an import/export format or for external preference files, etc, it is becoming ever more important to have a suite of good XML tools in 4D. Expat4D gives a good XML parser to 4D developers in plug-in form. xmlObj will hopefully provide a useful interface to manipulating XML once it's parsed and for creating XML documents for export, reporting and a multitude of other uses.
xmlObj represents each node of an XML document as a separate object. Using ObjectTools, it's not necessary to define a class for these objects which makes it very flexible. Each node has references to it's parent node, it's next and previous siblings, any child nodes, and the root object. The root object is not the root element. It's the document root and so contains the XML declaration details and any processing instructions which appear before the root element, and the root element itself.
Take a look at the XML sample below. If you created an object from this document the xmlObj module would actually create 8 objects. i.e.
Although 8 objects are created, just one object reference is returned, the root object, and from this object the rest of the objects can be navigated.
<?xml version="1.0" encoding="utf-8"?> <!-- document which will convert to 8 xmlObjects --> <document> <person ID="p000666"> <name>Mark Mitchenall</name> <email>mark@mitchenall.com</email> </person> </document>
Assuming the above XML is stored in a BLOB called xml_BLOB, the following 4D code would return the root object of the xmlObject created from the BLOB.
C_BLOB(xml_BLOB) C_LONGINT($root) $root:=xmlObj_BlobToObject(->xml_BLOB)
As you can see, using xmlObj is fairly painless. Saving an object back into a BLOB, text variable or document is just as painless.
$outputText:=xmlObj_ObjectToText($root) xmlObj_ObjectToBlob($root;->outputBLOB) xmlObj_ObjectToDocument($root;$filePath)
| > Expat4D Home > Documentation |