XML Parsing and Validation
The two most common tasks that you’ll perform on your XML content are likely to be parsing and validating. If you’ve ever combed around on CPAN for XML-related modules, you probably already know that there’s no shortage of resources when it comes to Perl and XML. On the contrary, the sheer volume of modules available for Perl and XML is rather daunting, so you might be looking for a place to start.
This chapter covers two Perl/XML modules in particular: XML::Simple and XML::Parser. These modules were selected because they allow you to parse and manipulate most XML. While these modules themselves don’t validate XML, we’ll resort to a little bit of trickery to show you how you can do exactly that.
XML::Simple provides an easy API that allows you to read
and write XML. It is built on top of XML::Parser, which will be
covered shortly. As its name suggests, XML::Simple implements only two
methods: XMLin( )
and XMLout( )
. But
don’t let its apparent lack of methods fool you; XML::Simple lets you
do great things simply, such as parsing XML-written configuration
files.
Let’s say that your company keeps a log of its Sun Microsystems servers and their respective operation-system versions, IP addresses, and current patch levels. While you could keep this information in your home directory as a delimited file (which you can parse and analyze when you need to, or import into a database, such as PostGresSQL or MySQL), why not just write it in XML? You’ll find that by writing ...
Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.