As I mentioned earlier, there are two types of XML data ”static XML documents and dynamically generated XML documents. Two approaches to generating XML data also exist. I call them the "crazy" approach and the "lazy" approach. Note Remember, XML data can consist of a standalone file or XML data in memory (for example, dynamically generated XML data that is sent across a network). Using the Perl print Function to Generate XML DataThe crazy approach refers to using Perl's print function to print out all parts of the XML document. Because an XML document is just plain text, it can be easily generated using the standard Perl print function. In the next few sections, we'll take a closer look at the advantages and disadvantages to this approach. Advantages of Using the Perl print FunctionA few (but not too many) advantages exist for using the Perl print function to generate your XML documents.
Disadvantages of Using the Perl print FunctionAs you will soon see, there are more disadvantages than advantages to using this approach.
If you're going to be generating anything other than the simplest XML documents, I strongly suggest using an XML writer module. Using a Perl Module to Generate XML DataThe second method of generating data, which I call the lazy approach (also considered the smarter approach), is the easier approach of the two. How do you do it? Simply use a Perl module that was designed specifically for the task. The module takes care of the standard syntax and only expects you to pass it data. In this situation, your only responsibility is to determine what information should appear in the XML data, while the Perl module manages the XML- related tasks (for example, verifying matching start and end tags). A major advantage of using a Perl module to generate XML data is that it abstracts (and almost hides) the difficult standards-related tasks from the user. The lazy programmer is the one who takes the easiest approach to finishing the task ”using a writer module. Although an XML writer module may be considered by some to be overkill for generating a simple XML document, it definitely provides a cleaner, more general approach to generating XML data. Let's take a look at a few advantages and disadvantages associated with using a Perl module to generate XML data. Advantages of an XML-Generating ModuleThe following are some advantages of using the Perl module:
Disadvantage of an XML-Generating ModuleThe big disadvantage here is that some time will be required to become familiar with the module, but most module APIs are usually very straightforward. I've discussed a few reasons why you would want to use a Perl module to assist you in generating XML data. Several Perl modules exist that have been specifically developed to generate XML data. In the next section, you'll take a look at examples that demonstrate each of the approaches to generating XML data that we just discussed. |