/----------------------------------------------------\ | Generating and Parsing XML with Perl -- A Tutorial | | (C)2001-2004 Adam Monsen | \----------------------------------------------------/ Perl is well-suited for XML manipulation. Here's one way to generate and retrieve information from XML. --BEGIN xml_gen.pl-------------------------------- #!/usr/bin/perl -w use XML::Generator; use strict; my $xml = XML::Generator->new(':pretty'); print $xml->xmldecl(); print $xml->DataTribe( $xml->SendFunc( $xml->AcknowledgeAddrs( $xml->EmailAddress('amonsen@example.com'), $xml->EmailAddress('meonkeys@hotmail.com'), ), $xml->ClientName('MarketSuckemdry & Buy, INC.'), $xml->ImportData( $xml->Row( $xml->UserID('0157H7'), $xml->Column({ Name => 'Car'}, 'Honda'), $xml->Column({ Name => 'Sex'}, 'Female'), ), ), ), ); print "\n"; --END xml_gen.pl-------------------------------- Close all your parens properly and you're guaranteed well-formed XML! This is basically a compile-time check for well-formedness. XML::Generator also has easy ways to handle namespaces, CDATA, comments, amonsen@example.com meonkeys@hotmail.com MarketSuckemdry & Buy, INC. 0157H7 Honda Female --END xml_gen.pl output------------------------- For checking XML validity against a DTD, I use nsgmls. I don't know an easy way to validate XML with Perl. For Retrieving data, XML::XPath is a great way to get at a specific piece of data. I ran this script on the output of xml_gen.pl... --BEGIN xpath.pl---------------------------------- #!/usr/bin/perl -w use XML::XPath; use XML::XPath::XMLParser; use strict; my $xpath = XML::XPath->new(ioref => \*STDIN); my $clientname = $xpath->find('/DataTribe/SendFunc/ClientName/text()'); print "Client is $clientname for this transaction\n"; --END xpath.pl---------------------------------- And here is the result. --BEGIN xpath.pl output--------------------------- Client is MarketSuckemdry & Buy, INC. for this transaction --END xpath.pl output--------------------------- Notice how the amperstand was conveniently un-escaped. Good luck! -Adam /---------------------------------------------\ | v0.01 [ Mon Nov 26 19:23:37 PST 2001 ] | | -initial revision | | v0.02 [ Sun Feb 10 17:20:12 PST 2002 ] | | -simplified XML::XPath test (thanks Steve | | Grazzini!) | | -now in unix format | | -fixed some typos | | v0.03 [ Sun Apr 13 13:06:02 PDT 2003 ] | | -added xml declaration | | v0.04 [ Sat Mar 20 11:34:59 PST 2004 ] | | -update for XML::Generator v0.98, which | | includes a ':pretty' option (thanks | | Benjamin Holzman!) | \---------------------------------------------/