Welcome to the second part of a four-part series on Delphi and the DOM. In the first article, we discussed Delphi and XML. Now that you have some idea of the core elements of XML documents, we can talk about how to manipulate them in Delphi.
There are two ways in which you can manipulate XML documents:
Using the Document Object Model interface
Using the Simple API for XML or SAX
The two techniques could not be more different from each other.
The Document Object Model loads the entire document into a hierarchical tree of nodes, allowing you to read them and manipulate them to change the document. For this reason, the Document Object Model is suitable when you want to navigate the XML structure in memory and edit it, or for creating new documents from scratch.
The Simple API for XML or SAX parses the document, firing an event for each element without building a structure in memory. Once parsed by the Simple API for XML, the document is lost, but this operation is generally much faster than the construction of the Document Object Model tree. Using the Simple API for XML is good for reading an XML document once, i.e if you are looking for a portion of its data.
There is another way to manipulate or to create XML documents: string management. Creating a document by adding strings is the fastest operation, particularly if you can do a single pass (and don't need to modify nodes already generated). Reading documents by strings is very fast, but can become difficult for complex structures.
Delphi provides two more methods you should know about. The first is the definition of interfaces that map the document structure; this method is used to access the document instead of the generic DOM interface. This approach makes for faster coding and more robust applications.The second approach is the development of transformations that enable you to read a generic XML document into a ClientDataSet component or save the dataset into an XML file for a given structure.
Which of these two is better? Well, that you will have to decide based on your needs. So how do we code for DOM in Delphi? That is what we will be looking at next.