Name : perl-XML-Generator
| |
Version : 1.13
| Vendor : obs://build_opensuse_org/devel:languages:perl
|
Release : lp154.1.1
| Date : 2023-04-30 07:43:51
|
Group : Unspecified
| Source RPM : perl-XML-Generator-1.13-lp154.1.1.src.rpm
|
Size : 0.11 MB
| |
Packager : https://www_suse_com/
| |
Summary : Perl extension for generating XML
|
Description :
In general, once you have an XML::Generator object, you then simply call methods on that object named for each XML tag you wish to generate.
XML::Generator can also arrange for undefined subroutines in the caller\'s package to generate the corresponding XML, by exporting an \'AUTOLOAD\' subroutine to your package. Just supply an \':import\' argument to your \'use XML::Generator;\' call. If you already have an \'AUTOLOAD\' defined then XML::Generator can be configured to cooperate with it. See \"STACKABLE AUTOLOADs\".
Say you want to generate this XML:
< person> < name>Bob< /name> < age>34< /age> < job>Accountant< /job> < /person>
Here\'s a snippet of code that does the job, complete with pretty printing:
use XML::Generator; my $gen = XML::Generator->new(\':pretty\'); print $gen->person( $gen->name(\"Bob\"), $gen->age(34), $gen->job(\"Accountant\") );
The only problem with this is if you want to use a tag name that Perl\'s lexer won\'t understand as a method name, such as \"shoe-size\". Fortunately, since you can store the name of a method in a variable, there\'s a simple work-around:
my $shoe_size = \"shoe-size\"; $xml = $gen->$shoe_size(\"12 1/2\");
Which correctly generates:
< shoe-size>12 1/2< /shoe-size>
You can use a hash ref as the first parameter if the tag should include atributes. Normally this means that the order of the attributes will be unpredictable, but if you have the Tie::IxHash module, you can use it to get the order you want, like this:
use Tie::IxHash; tie my %attr, \'Tie::IxHash\';
%attr = (name => \'Bob\', age => 34, job => \'Accountant\', \'shoe-size\' => \'12 1/2\');
print $gen->person(\\%attr);
This produces
< person name=\"Bob\" age=\"34\" job=\"Accountant\" shoe-size=\"12 1/2\" />
An array ref can also be supplied as the first argument to indicate a namespace for the element and the attributes.
If there is one element in the array, it is considered the URI of the default namespace, and the tag will have an xmlns=\"URI\" attribute added automatically. If there are two elements, the first should be the tag prefix to use for the namespace and the second element should be the URI. In this case, the prefix will be used for the tag and an xmlns:PREFIX attribute will be automatically added. Prior to version 0.99, this prefix was also automatically added to each attribute name. Now, the default behavior is to leave the attributes alone (although you may always explicitly add a prefix to an attribute name). If the prior behavior is desired, use the constructor option \'qualified_attributes\'.
If you specify more than two elements, then each pair should correspond to a tag prefix and the corresponding URL. An xmlns:PREFIX attribute will be added for each pair, and the prefix from the first such pair will be used as the tag\'s namespace. If you wish to specify a default namespace, use \'#default\' for the prefix. If the default namespace is first, then the tag will use the default namespace itself.
If you want to specify a namespace as well as attributes, you can make the second argument a hash ref. If you do it the other way around, the array ref will simply get stringified and included as part of the content of the tag.
Here\'s an example to show how the attribute and namespace parameters work:
$xml = $gen->account( $gen->open([\'transaction\'], 2000), $gen->deposit([\'transaction\'], { date => \'1999.04.03\'}, 1500) );
This generates:
< account> < open xmlns=\"transaction\">2000< /open> < deposit xmlns=\"transaction\" date=\"1999.04.03\">1500< /deposit> < /account>
Because default namespaces inherit, XML::Generator takes care to output the xmlns=\"URI\" attribute as few times as strictly necessary. For example,
$xml = $gen->account( $gen->open([\'transaction\'], 2000), $gen->deposit([\'transaction\'], { date => \'1999.04.03\'}, $gen->amount([\'transaction\'], 1500) ) );
This generates:
< account> < open xmlns=\"transaction\">2000< /open> < deposit xmlns=\"transaction\" date=\"1999.04.03\"> < amount>1500< /amount> < /deposit> < /account>
Notice how \'xmlns=\"transaction\"\' was left out of the \'< amount\'> tag.
Here is an example that uses the two-argument form of the namespace:
$xml = $gen->widget([\'wru\' => \'http://www.widgets-r-us.com/xml/\'], {\'id\' => 123}, $gen->contents());
< wru:widget xmlns:wru=\"http://www.widgets-r-us.com/xml/\" id=\"123\"> < contents /> < /wru:widget>
Here is an example that uses multiple namespaces. It generates the first example from the RDF primer (http://www.w3.org/TR/rdf-primer/).
my $contactNS = [contact => \"http://www.w3.org/2000/10/swap/pim/contact#\"]; $xml = $gen->xml( $gen->RDF([ rdf => \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\", AATT$contactNS ], $gen->Person($contactNS, { \'rdf:about\' => \"http://www.w3.org/People/EM/contact#me\" }, $gen->fullName($contactNS, \'Eric Miller\'), $gen->mailbox($contactNS, {\'rdf:resource\' => \"mailto:emAATTw3.org\"}), $gen->personalTitle($contactNS, \'Dr.\'))));
< ?xml version=\"1.0\" standalone=\"yes\"?> < rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:contact=\"http://www.w3.org/2000/10/swap/pim/contact#\"> < contact:Person rdf:about=\"http://www.w3.org/People/EM/contact#me\"> < contact:fullName>Eric Miller< /contact:fullName> < contact:mailbox rdf:resource=\"mailto:emAATTw3.org\" /> < contact:personalTitle>Dr.< /contact:personalTitle> < /Person> < /rdf:RDF>
|
RPM found in directory: /packages/linux-pbone/ftp5.gwdg.de/pub/opensuse/repositories/devel:/languages:/perl:/CPAN-X/15.4/noarch |