1SAX(3) User Contributed Perl Documentation SAX(3)
2
3
4
6 XML::SAX - Simple API for XML
7
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
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
29 The factory class is XML::SAX::ParserFactory. Please see the documenta‐
30 tion 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
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 provid‐
50 ing a number of methods automagically for you. See XML::SAX::Base for
51 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 WriteMakefile(
63 ...
64 PREREQ_PM => { 'XML::SAX' => 0 },
65 ...
66 );
67
68 Alternatively you may wish to check for it in other ways that will
69 cause more than just a warning.
70
71 * Add the following code snippet to your Makefile.PL:
72 sub MY::install {
73 package MY;
74 my $script = shift->SUPER::install(@_);
75 if (ExtUtils::MakeMaker::prompt(
76 "Do you want to modify ParserDetails.ini?", 'Y')
77 =~ /^y/i) {
78 $script =~ s/install :: (.*)$/install :: $1 install_sax_driver/m;
79 $script .= <<"INSTALL";
80
81 install_sax_driver :
82 \t\@\$(PERL) -MXML::SAX -e "XML::SAX->add_parser(q(\$(NAME)))->save_parsers()"
83
84 INSTALL
85 }
86 return $script;
87 }
88
89 Note that you should check the output of this - \$(NAME) will use
90 the name of your distribution, which may not be exactly what you
91 want. For example XML::LibXML has a driver called
92 XML::LibXML::SAX::Generator, which is used in place of \$(NAME) in
93 the above.
94
95 * Add an XML::SAX test:
96 A test file should be added to your t/ directory containing some‐
97 thing like the following:
98
99 use Test;
100 BEGIN { plan tests => 3 }
101 use XML::SAX;
102 use XML::SAX::PurePerl::DebugHandler;
103 XML::SAX->add_parser(q(XML::SAX::MyDriver));
104 local $XML::SAX::ParserPackage = 'XML::SAX::MyDriver';
105 eval {
106 my $handler = XML::SAX::PurePerl::DebugHandler->new();
107 ok($handler);
108 my $parser = XML::SAX::ParserFactory->parser(Handler => $handler);
109 ok($parser);
110 ok($parser->isa('XML::SAX::MyDriver');
111 $parser->parse_string("<tag/>");
112 ok($handler->{seen}{start_element});
113 };
114
116 By default, XML::SAX exports nothing into the caller's namespace. How‐
117 ever you can request the symbols "Namespaces" and "Validation" which
118 are the URIs for those features, allowing an easier way to request
119 those features via ParserFactory:
120
121 use XML::SAX qw(Namespaces Validation);
122 my $factory = XML::SAX::ParserFactory->new();
123 $factory->require_feature(Namespaces);
124 $factory->require_feature(Validation);
125 my $parser = $factory->parser();
126
128 Current maintainer: Grant McLean, grantm@cpan.org
129
130 Originally written by:
131
132 Matt Sergeant, matt@sergeant.org
133
134 Kip Hampton, khampton@totalcinema.com
135
136 Robin Berjon, robin@knowscape.com
137
139 This is free software, you may use it and distribute it under the same
140 terms as Perl itself.
141
143 XML::SAX::Base for writing SAX Filters and Parsers
144
145 XML::SAX::PurePerl for an XML parser written in 100% pure perl.
146
147 XML::SAX::Exception for details on exception handling
148
149
150
151perl v5.8.8 2005-10-14 SAX(3)