use XML::Pastor; my $pastor = XML::Pastor->new(); # Generate MULTIPLE modules, one module for each class, and put them + under destination. $pastor->generate( mode =>'offline', style => 'multiple', schema=>'/some/path/to/schema.xsd', class_prefix=>'MyApp::Data::', destination=>'/tmp/lib/perl/', ); # Generate a SINGLE module which contains all the classes and put it + under destination. # Note that the schema may be read from a URL too. $pastor->generate( mode =>'offline', style => 'single', schema=>'http://some/url/to/schema.xsd', class_prefix=>'MyApp::Data::', module => 'Module', destination=>'/tmp/lib/perl/', + ); # Generate classes in MEMORY, and EVALUATE the generated code on the + fly. # (Run Time code generation) $pastor->generate( mode =>'eval', schema=>'/some/path/to/schema.xsd', class_prefix=>'MyApp::Data::' ); # Same thing, with a maximum of DEBUG output on STDERR $pastor->generate( mode =>'eval', schema=>'/some/path/to/schema.xsd', class_prefix=>'MyApp::Data::', verbose = 9 );
And somewhere in an other place of the code ... (Assuming a global XML element 'country' existed in you schema and hence been generated by Pastor).
my $country = MyApp::Data::country->from_xml_file('/some/path/to/cou +ntry.xml'); # retrieve from a file $country = MyApp::Data::country->from_xml_url('http://some/url/to/co +untry.xml'); # or from a URL $country = MyApp::Data::country->from_xml_fh($fh); # or from a f +ile handle $country = MyApp::Data::country->from_xml_dom($dom); # or from DO +M (a XML::LibXML::Node or XML::LibXML::Document) # or from an XML string $country = MyApp::Data::country->from_xml_string(<<'EOF'); <?xml version="1.0"?> <country code="FR" name="France"> <city code="PAR" name="Paris"/> <city code="LYO" name="Lyon"/> </country> EOF # or if you don't know if you have a file, URL, FH, or string $country = MyApp::Data::country->from_xml('http://some/url/to/countr +y.xml'); # Now you can manipulate your country object. print $country->name; # prints "France" print $country->city->[0]->name; # prints "Paris" # Let's make some changes $country->code('fr'); $country->name('FRANCE'); my $class=$country->xml_field_class('city'); my $city = $class->new(); $city->code('MRS'); $city->name('Marseille'); push @{$country->city}, $city; print $country->city->[2]->name; # prints "Marseille" # Time to validate our XML $country->xml_validate(); # This one will DIE on failure if ($country->is_xml_valid()) { # This one will not die. print "ok\n"; }else { print "Validation error : $@\n"; # Note that $@ contains the erro +r message } # Time to write the the object back to XML $country->to_xml_file('some/path/to/country.xml'); # To a fil +e $country->to_xml_url('http://some/url/to/country.xml'); # To a UR +L $country->to_xml_fh($fh); # To a FILE HANDLE my $dom=$country->to_xml_dom(); # To a DOM Node (XML::Lib +XML::Node) my $dom=$country->to_xml_dom_document(); # To a DOM Document (XM +L::LibXML::Document) my $xml=$country->to_xml_string(); # To a string my $frag=$country->to_xml_fragment(); # Same thing without th +e <?xml version="1.0?> part
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |