Skip to content

Commit e946130

Browse files
committed
pod2markdown Parser.pm > README.md
1 parent 9ab8d82 commit e946130

File tree

1 file changed

+53
-15
lines changed

1 file changed

+53
-15
lines changed

README.md

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[![Build Status](https://github.com/cpan-authors/XML-Parser/actions/workflows/testsuite.yml/badge.svg)](https://github.com/cpan-authors/XML-Parser/actions/workflows/testsuite.yml)
2+
23
# NAME
34

45
XML::Parser - A perl module for parsing XML documents
@@ -32,7 +33,7 @@ XML::Parser - A perl module for parsing XML documents
3233
# DESCRIPTION
3334

3435
This module provides ways to parse XML documents. It is built on top of
35-
[XML::Parser::Expat](https://metacpan.org/pod/XML::Parser::Expat), which is a lower level interface to James Clark's
36+
[XML::Parser::Expat](https://metacpan.org/pod/XML%3A%3AParser%3A%3AExpat), which is a lower level interface to James Clark's
3637
expat library. Each call to one of the parsing methods creates a new
3738
instance of XML::Parser::Expat which is then used to parse the document.
3839
Expat options may be provided when the XML::Parser object is created.
@@ -101,7 +102,7 @@ the _Expat_ object, not the Parser object.
101102
- Namespaces
102103

103104
This is an Expat option. If this is set to a true value, then namespace
104-
processing is done during the parse. See ["Namespaces" in XML::Parser::Expat](https://metacpan.org/pod/XML::Parser::Expat#Namespaces)
105+
processing is done during the parse. See ["Namespaces" in XML::Parser::Expat](https://metacpan.org/pod/XML%3A%3AParser%3A%3AExpat#Namespaces)
105106
for further discussion of namespace processing.
106107

107108
- NoExpand
@@ -163,14 +164,17 @@ the _Expat_ object, not the Parser object.
163164
or whatever is returned from the **Final** handler, if one is installed.
164165
In other words, what parse may return depends on the style.
165166

167+
See ["ERROR HANDLING"](#error-handling) below for how to catch and handle parse errors.
168+
166169
- parsestring
167170

168171
This is just an alias for parse for backwards compatibility.
169172

170173
- parsefile(FILE \[, OPT => OPT\_VALUE \[...\]\])
171174

172175
Open FILE for reading, then call parse with the open handle. The file
173-
is closed no matter how parse returns. Returns what parse returns.
176+
is closed no matter how parse returns. A die call is thrown if the file
177+
cannot be opened or if a parse error occurs. Returns what parse returns.
174178

175179
- parse\_start(\[ OPT => OPT\_VALUE \[...\]\])
176180

@@ -191,7 +195,7 @@ Expat is an event based parser. As the parser recognizes parts of the
191195
document (say the start or end tag for an XML element), then any handlers
192196
registered for that type of an event are called with suitable parameters.
193197
All handlers receive an instance of XML::Parser::Expat as their first
194-
argument. See ["METHODS" in XML::Parser::Expat](https://metacpan.org/pod/XML::Parser::Expat#METHODS) for a discussion of the
198+
argument. See ["METHODS" in XML::Parser::Expat](https://metacpan.org/pod/XML%3A%3AParser%3A%3AExpat#METHODS) for a discussion of the
195199
methods that can be called on this object.
196200

197201
## Init (Expat)
@@ -228,12 +232,10 @@ more consecutive Char events. This typically occurs with files larger than
228232
about 32 KiB and is not a bug. To obtain the complete text of an element,
229233
accumulate the strings delivered between Start and End events:
230234

231-
```perl
232-
my $current_text;
233-
sub start_handler { $current_text = ''; }
234-
sub char_handler { $current_text .= $_[1]; }
235-
sub end_handler { print "complete text: $current_text\n"; }
236-
```
235+
my $current_text;
236+
sub start_handler { $current_text = ''; }
237+
sub char_handler { $current_text .= $_[1]; }
238+
sub end_handler { print "complete text: $current_text\n"; }
237239

238240
The Stream style (`XML::Parser::Style::Stream`) already performs this
239241
accumulation automatically.
@@ -339,7 +341,7 @@ set, then this handler will not be called for unparsed entities.
339341

340342
The element handler is called when an element declaration is found. Name
341343
is the element name, and Model is the content model as an XML::Parser::Content
342-
object. See ["XML::Parser::ContentModel Methods" in XML::Parser::Expat](https://metacpan.org/pod/XML::Parser::Expat#XML::Parser::ContentModel-Methods)
344+
object. See ["XML::Parser::ContentModel Methods" in XML::Parser::Expat](https://metacpan.org/pod/XML%3A%3AParser%3A%3AExpat#XML::Parser::ContentModel-Methods)
343345
for methods available for this class.
344346

345347
## Attlist (Expat, Elname, Attname, Type, Default, Fixed)
@@ -376,9 +378,8 @@ including any internal or external DTD declarations.
376378

377379
This handler is called for xml declarations. Version is a string containing
378380
the version. Encoding is either undefined or contains an encoding string.
379-
Standalone is either undefined, or the string `"yes"` or `"no"`.
380-
Undefined indicates that no standalone parameter was given in the XML
381-
declaration.
381+
Standalone will be either the string `"yes"`, `"no"`, or undefined if the
382+
standalone attribute is yes, no, or not made respectively.
382383

383384
# STYLES
384385

@@ -497,6 +498,37 @@ finds, it loads.
497498
If you wish to build your own encoding maps, check out the XML::Encoding
498499
module from CPAN.
499500

501+
# ERROR HANDLING
502+
503+
XML::Parser throws an exception (dies) when it encounters a parse error.
504+
This includes malformed XML, encoding errors, and other problems detected
505+
by the underlying expat library.
506+
507+
The `parse`, `parsefile`, and `parse_done` methods may all throw
508+
exceptions. To handle parse errors gracefully in your application, wrap
509+
the parse call in an `eval` block:
510+
511+
my $parser = XML::Parser->new(Style => 'Tree');
512+
513+
my $tree = eval { $parser->parsefile('data.xml') };
514+
if ($@) {
515+
# Handle the parse error
516+
warn "Parse failed: $@";
517+
}
518+
519+
The error message (in `$@`) will include the line number, column number,
520+
and byte position where the error was detected. For additional context
521+
around the error location, set the **ErrorContext** option when constructing
522+
the parser:
523+
524+
my $parser = XML::Parser->new(
525+
Style => 'Tree',
526+
ErrorContext => 2,
527+
);
528+
529+
This will include 2 lines of context on either side of the error in the
530+
error message.
531+
500532
# AUTHORS
501533

502534
Larry Wall <`larry@wall.org`> wrote version 1.0.
@@ -505,4 +537,10 @@ Clark Cooper <`coopercc@netheaven.com`> picked up support, changed the API
505537
for this version (2.x), provided documentation,
506538
and added some standard package features.
507539

508-
Matt Sergeant <`matt@sergeant.org`> is now maintaining XML::Parser
540+
Matt Sergeant <`matt@sergeant.org`> was maintaining XML::Parser from 2003 to 2007.
541+
542+
Alexandr Ciornii <`alexchorny@gmail.com`> was maintaining XML::Parser from 2007 to 2013.
543+
544+
Todd Rinaldo <`toddr@cpan.org`> has been maintaining XML::Parser since 2013.
545+
546+
The project started making use of Claude Code <`https://claude.ai/code`> in January 2026.

0 commit comments

Comments
 (0)