Different Methods of Generating XML Data


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 Data

The 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 Function

A few (but not too many) advantages exist for using the Perl print function to generate your XML documents.

  • It's simple. Just use the Perl print function to generate XML documents.

  • It can be performed very quickly with basic Perl skills. Perl's print function is quick and easy to use for short, simple XML documents.

  • Your application will not have external module dependencies. One possible situation when this could be important is in the case of a production server. Let's say that you're working on the server; however, because it's the production server, you're restricted from installing any modules.

Disadvantages of Using the Perl print Function

As you will soon see, there are more disadvantages than advantages to using this approach.

  • In this case, the user may be responsible for escaping attribute values and character data (not a problem if you use a "Here" document). The first line of an XML document contains <?xml version="1.0"?> . If you use the Perl print function to generate XML data, then you're required to escape any quotation marks. For example, version="1.0" must be escaped to version=\"1.0\" because it appears inside of a print statement.

  • This method doesn't perform any checks to verify that the resulting XML document is well-formed. So, it is your responsibility to make sure that the generated XML document is well- formed . The user is responsible for verifying that there is an end tag that matches each start tag. This can begin to get confusing if you have a complicated, nested XML document.

  • The user is responsible for generating the constructs for different types of markup (for example, tags, comments, and processing instructions).

  • The resulting program can be difficult to maintain or modify ( especially if the application is passed on to another developer).

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 Data

The 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 Module

The following are some advantages of using the Perl module:

  • Modules perform some error checking to verify that generated XML data is well-formed (for example, it matches start and end tags). Typically, the module will generate an error if you forget a closing tag or improperly try to nest elements.

  • Long- term maintenance is easier.

  • The source code is shorter, cleaner, and easier to understand if you build an application based on a module.

Disadvantage of an XML-Generating Module

The 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.



XML and Perl
XML and Perl
ISBN: 0735712891
EAN: 2147483647
Year: 2002
Pages: 145

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net