XML::Writer::Compiler - create XML::Writer based classes whose instances can generate, refine and extend sample XML
ROOT=CustomerAdd XML_FILE=$ROOT.xml OUTPUT_PKG = XML::Quickbooks::$ROOT HASH_DEPTH=4 #counting from 0, which level of XML file should hash keys start mapping from OUTPATH_PREPEND=lib EXTENDS=XML::Quickbooks # the XML class we generate, XML::Quickbooks::CustomerAdd has the parent XML::Quickbooks # Now run the compiler on the XML file to produce an XML class, which produces XML when a hashref is supplied # Also possible to subclass the generated XML class to customize XML generation when hashrefs are inadequate xwc $XML_FILE $OUTPUT_PKG $HASH_DEPTH $OUTPUT_PREPEND $EXTENDS
XML::Writer::Compiler is a module which takes a sample XML document and creates a single class from it. This class contains methods for each tag of the XML. The instance of the class can generate XML with the content supplied via a hashref. Subclassing the generated class allows more precise control over how the object-oriented XML generation occurs.
The CPAN module most similar to XML::Writer::Compiler is XML::Toolkit.
<note> <to> <person> Bob </person> </to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
This step is normally done via the xwc script, but you can also write Perl code to compile XML:
my $compiler = XML::Writer::Compiler->new; my $tree = XML::TreeBuilder->new( { 'NoExpand' => 0, 'ErrorContext' => 0 } ); $tree->parse_file('t/sample.xml'); my $pkg = XML::Note'; my $class = $compiler->buildclass( $pkg, $tree, 0, '' );
my %data = ( note => { to => { person => 'Satan' }, from => [ [ via => 'postcard', russia => 'with love' ], 'moneypenny' ] } ); my $xml = XML::Note->new; $xml->data(\%data); warn $xml->xml->string->value;
If a simple substitution from a hashref of data will not suffice, then you can take the methods of the generated class and subclass them for things like repeating or conditional content. See the test file repeating.t for an example.
The xwc script is the most common way to convert an XML file to an equivalent Perl class. The script takes the following arguments
the full/relative path to the sample XML file
the name of the Perl package that will represent the xml_file
xml_file
An XML file has nesting levels, or depth. Oftentimes, the outer levels will never be rewritten via the data hashref you supply. As a result, you dont want to have to several levels of your hashref before you actually specify the data you want to bind.
Concretely, let's take some XML from the Quickbooks SDK: https://member.developer.intuit.com/qbSDK-current/Common/newOSR/index.html
For instance the CustomerAdd xml starts like this:
<QBXML> <QBXMLMsgsRq onError="stopOnError"> <CustomerAddRq> <CustomerAdd> <!-- required --> <Name >STRTYPE</Name> <!-- required --> <IsActive >BOOLTYPE</IsActive> .... </QBXML>
Now, none of the XML from the root to the XPath QBXML/QBXMLMsgsRq/CustomerAddRq/CustomerAdd needs any binding from the hashref. If you compiled this XML and had hash_depth>> set to 0, then to set the name your hashref would have to look like:
QBXML/QBXMLMsgsRq/CustomerAddRq/CustomerAdd
hash_depth>> set to 0, then to set the name your hashref would have to look like:
my %data = ( QBXML => { QXBMLMsgsRq => { CustomerAddRq => { CustomerAdd => { Name => 'Bob Jones' }}}}} ;
In contrast , if you compiled this XML and had hash_depth>> set to 4, then to set the name your hashref would only have to look like:
hash_depth>> set to 4, then to set the name your hashref would only have to look like:
my %data = ( Name => 'Bob Jones' } ;
The generated class file has a path generated from splitting the class name on double colon. In most cases, you will want to write the class file to a path with 'lib' prepended and so you would set this option to 'lib'
The XML class file is a Moose class and can extend any superclass. E.g., in the XML::Quickbooks distribution, each XML class file extends XML::Quickbooks.
XML::Quickbooks
https://github.com/metaperl/xml-writer-compiler
XML::Toolkit
XML::Element::Tolol
http://perlmonks.org/index.pl?node_id=910617
http://perlmonks.org/index.pl?node_id=909187
http://perlmonks.org/index.pl?node_id=913713
Copyright RANGE('2011-07-25', NOW()) Terrence Brannon.
RANGE('2011-07-25', NOW())
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
Terrence Brannon <tbone@cpan.org>
2 POD Errors
The following errors were encountered while parsing the POD:
Unterminated C< ... > sequence
To install XML::Writer::Compiler, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XML::Writer::Compiler
CPAN shell
perl -MCPAN -e shell install XML::Writer::Compiler
For more information on module installation, please visit the detailed CPAN module installation guide.