1SAX::ParserFactory(3) User Contributed Perl DocumentationSAX::ParserFactory(3)
2
3
4
6 XML::SAX::ParserFactory - Obtain a SAX parser
7
9 use XML::SAX::ParserFactory;
10 use XML::SAX::XYZHandler;
11 my $handler = XML::SAX::XYZHandler->new();
12 my $p = XML::SAX::ParserFactory->parser(Handler => $handler);
13 $p->parse_uri("foo.xml");
14 # or $p->parse_string("<foo/>") or $p->parse_file($fh);
15
17 XML::SAX::ParserFactory is a factory class for providing an application
18 with a Perl SAX2 XML parser. It is akin to DBI - a front end for other
19 parser classes. Each new SAX2 parser installed will register itself
20 with XML::SAX, and then it will become available to all applications
21 that use XML::SAX::ParserFactory to obtain a SAX parser.
22
23 Unlike DBI however, XML/SAX parsers almost all work alike (especially
24 if they subclass XML::SAX::Base, as they should), so rather than
25 specifying the parser you want in the call to "parser()", XML::SAX has
26 several ways to automatically choose which parser to use:
27
28 · $XML::SAX::ParserPackage
29
30 If this package variable is set, then this package is "require()"d
31 and an instance of this package is returned by calling the "new()"
32 class method in that package. If it cannot be loaded or there is an
33 error, an exception will be thrown. The variable can also contain a
34 version number:
35
36 $XML::SAX::ParserPackage = "XML::SAX::Expat (0.72)";
37
38 And the number will be treated as a minimum version number.
39
40 · Required features
41
42 It is possible to require features from the parsers. For example,
43 you may wish for a parser that supports validation via a DTD. To do
44 that, use the following code:
45
46 use XML::SAX::ParserFactory;
47 my $factory = XML::SAX::ParserFactory->new();
48 $factory->require_feature('http://xml.org/sax/features/validation');
49 my $parser = $factory->parser(...);
50
51 Alternatively, specify the required features in the call to the
52 ParserFactory constructor:
53
54 my $factory = XML::SAX::ParserFactory->new(
55 RequiredFeatures => {
56 'http://xml.org/sax/features/validation' => 1,
57 }
58 );
59
60 If the features you have asked for are unavailable (for example the
61 user might not have a validating parser installed), then an
62 exception will be thrown.
63
64 The list of known parsers is searched in reverse order, so it will
65 always return the last installed parser that supports all of your
66 requested features (Note: this is subject to change if someone
67 comes up with a better way of making this work).
68
69 · SAX.ini
70
71 ParserFactory will search @INC for a file called SAX.ini, which is
72 in a simple format:
73
74 # a comment looks like this,
75 ; or like this, and are stripped anywhere in the file
76 key = value # SAX.in contains key/value pairs.
77
78 All whitespace is non-significant.
79
80 This file can contain either a line:
81
82 ParserPackage = MyParserModule (1.02)
83
84 Where MyParserModule is the module to load and use for the parser,
85 and the number in brackets is a minimum version to load.
86
87 Or you can list required features:
88
89 http://xml.org/sax/features/validation = 1
90
91 And each feature with a true value will be required.
92
93 · Fallback
94
95 If none of the above works, the last parser installed on the user's
96 system will be used. The XML::SAX package ships with a pure perl
97 XML parser, XML::SAX::PurePerl, so that there will always be a
98 fallback parser.
99
101 Matt Sergeant, matt@sergeant.org
102
104 This is free software, you may use it and distribute it under the same
105 terms as Perl itself.
106
107
108
109perl v5.26.3 2009-10-10 SAX::ParserFactory(3)