1XML::SAX(3)           User Contributed Perl Documentation          XML::SAX(3)
2
3
4

NAME

6       XML::SAX - Simple API for XML
7

SYNOPSIS

9         use XML::SAX;
10
11         # get a list of known parsers
12         my $parsers = XML::SAX->parsers();
13
14         # add/update a parser
15         XML::SAX->add_parser(q(XML::SAX::PurePerl));
16
17         # remove parser
18         XML::SAX->remove_parser(q(XML::SAX::Foodelberry));
19
20         # save parsers
21         XML::SAX->save_parsers();
22

DESCRIPTION

24       XML::SAX is a SAX parser access API for Perl. It includes classes and
25       APIs required for implementing SAX drivers, along with a factory class
26       for returning any SAX parser installed on the user's system.
27

USING A SAX2 PARSER

29       The factory class is XML::SAX::ParserFactory. Please see the
30       documentation of that module for how to instantiate a SAX parser:
31       XML::SAX::ParserFactory. However if you don't want to load up another
32       manual page, here's a short synopsis:
33
34         use XML::SAX::ParserFactory;
35         use XML::SAX::XYZHandler;
36         my $handler = XML::SAX::XYZHandler->new();
37         my $p = XML::SAX::ParserFactory->parser(Handler => $handler);
38         $p->parse_uri("foo.xml");
39         # or $p->parse_string("<foo/>") or $p->parse_file($fh);
40
41       This will automatically load a SAX2 parser (defaulting to
42       XML::SAX::PurePerl if no others are found) and return it to you.
43
44       In order to learn how to use SAX to parse XML, you will need to read
45       XML::SAX::Intro and for reference, XML::SAX::Specification.
46

WRITING A SAX2 PARSER

48       The first thing to remember in writing a SAX2 parser is to subclass
49       XML::SAX::Base. This will make your life infinitely easier, by
50       providing a number of methods automagically for you. See XML::SAX::Base
51       for more details.
52
53       When writing a SAX2 parser that is compatible with XML::SAX, you need
54       to inform XML::SAX of the presence of that driver when you install it.
55       In order to do that, XML::SAX contains methods for saving the fact that
56       the parser exists on your system to a "INI" file, which is then loaded
57       to determine which parsers are installed.
58
59       The best way to do this is to follow these rules:
60
61       ·   Add XML::SAX as a prerequisite in Makefile.PL:
62
63             WriteMakefile(
64                 ...
65                 PREREQ_PM => { 'XML::SAX' => 0 },
66                 ...
67             );
68
69           Alternatively you may wish to check for it in other ways that will
70           cause more than just a warning.
71
72       ·   Add the following code snippet to your Makefile.PL:
73
74             sub MY::install {
75               package MY;
76               my $script = shift->SUPER::install(@_);
77               if (ExtUtils::MakeMaker::prompt(
78                 "Do you want to modify ParserDetails.ini?", 'Y')
79                 =~ /^y/i) {
80                 $script =~ s/install :: (.*)$/install :: $1 install_sax_driver/m;
81                 $script .= <<"INSTALL";
82
83             install_sax_driver :
84             \t\@\$(PERL) -MXML::SAX -e "XML::SAX->add_parser(q(\$(NAME)))->save_parsers()"
85
86             INSTALL
87               }
88               return $script;
89             }
90
91           Note that you should check the output of this - \$(NAME) will use
92           the name of your distribution, which may not be exactly what you
93           want. For example XML::LibXML has a driver called
94           XML::LibXML::SAX::Generator, which is used in place of \$(NAME) in
95           the above.
96
97       ·   Add an XML::SAX test:
98
99           A test file should be added to your t/ directory containing
100           something like the following:
101
102             use Test;
103             BEGIN { plan tests => 3 }
104             use XML::SAX;
105             use XML::SAX::PurePerl::DebugHandler;
106             XML::SAX->add_parser(q(XML::SAX::MyDriver));
107             local $XML::SAX::ParserPackage = 'XML::SAX::MyDriver';
108             eval {
109               my $handler = XML::SAX::PurePerl::DebugHandler->new();
110               ok($handler);
111               my $parser = XML::SAX::ParserFactory->parser(Handler => $handler);
112               ok($parser);
113               ok($parser->isa('XML::SAX::MyDriver');
114               $parser->parse_string("<tag/>");
115               ok($handler->{seen}{start_element});
116             };
117

EXPORTS

119       By default, XML::SAX exports nothing into the caller's namespace.
120       However you can request the symbols "Namespaces" and "Validation" which
121       are the URIs for those features, allowing an easier way to request
122       those features via ParserFactory:
123
124         use XML::SAX qw(Namespaces Validation);
125         my $factory = XML::SAX::ParserFactory->new();
126         $factory->require_feature(Namespaces);
127         $factory->require_feature(Validation);
128         my $parser = $factory->parser();
129

AUTHOR

131       Current maintainer: Grant McLean, grantm@cpan.org
132
133       Originally written by:
134
135       Matt Sergeant, matt@sergeant.org
136
137       Kip Hampton, khampton@totalcinema.com
138
139       Robin Berjon, robin@knowscape.com
140

LICENSE

142       This is free software, you may use it and distribute it under the same
143       terms as Perl itself.
144

SEE ALSO

146       XML::SAX::Base for writing SAX Filters and Parsers
147
148       XML::SAX::PurePerl for an XML parser written in 100% pure perl.
149
150       XML::SAX::Exception for details on exception handling
151
152
153
154perl v5.30.1                      2020-01-30                       XML::SAX(3)
Impressum