XML::C...Parser(3pm)
π XML::Checker::Parser β the manual, but make it a group chat π
okay so bestie, welcome to the User Contributed Perl Documentation corner of the internet, where we're talking about XML::Checker::Parser, which lives in the XML::C...Parser(3pm) section. grab a strawberry π and let's go.
β¨ NAME β¨
XML::Checker::Parser β an XML::Parser that validates at parse time. like, while it's parsing. multitasking icon behavior fr fr π
π SYNOPSIS π
use XML::Checker::Parser;
my %expat_options = (KeepCDATA => 1,
Handlers => [ Unparsed => \&my_Unparsed_handler ]);
my $parser = new XML::Checker::Parser (%expat_options);
eval {
local $XML::Checker::FAIL = \&my_fail;
$parser->parsefile ("fail.xml");
};
if ($@) {
# Either XML::Parser (expat) threw an exception or my_fail() died.
... your error handling code here ...
}
# Throws an exception (with die) when an error is encountered, this
# will stop the parsing process.
# Don't die if a warning or info message is encountered, just print a message.
sub my_fail {
my $code = shift;
die XML::Checker::error_string ($code, @_) if $code < 200;
XML::Checker::print_error ($code, @_);
}
π¦ DESCRIPTION π¦
XML::Checker::Parser extends XML::Parser. that's the whole vibe. π
ngl I hope the example up in the SYNOPSIS says it all, because the move is literally just: use XML::Checker::Parser as if it were an XML::Parser. that's it. that's the tweet. See XML::Parser for the supported (expat) options. π
you can also derive your own parser from XML::Checker::Parser instead of from XML::Parser, and bestie the only thing you gotta do is swap this:
package MyParser;
@ISA = qw( XML::Parser );
for this:
package MyParser;
@ISA = qw( XML::Checker::Parser );
lowkey iconic how easy that is β¨
π XML::Checker::Parser constructor π
$parser = new XML::Checker::Parser (SkipExternalDTD => 1, SkipInsignifWS => 1);
the constructor takes the same parameters as XML::Parser, with these lil additions:
SkipExternalDTDβ by default, it will try to load external DTDs using LWP. you can turn that off by settingSkipExternalDTDto1. See External DTDs for the details, iykyk.SkipInsignifWSβ by default, it will treat insignificant whitespace as regularChardata. but if you setSkipInsignifWSto1, the userCharhandler will not be called when insignificant whitespace shows up. See"INSIGNIFICANT_WHITESPACE" in XML::Checkerfor the whole story.LWP_UserAgentβ when you callparsefile()with a URL (instead of a filename) or when loading external DTDs, we use LWP to download the remote file. by default it uses anLWP::UserAgentcreated like this:
use LWP::UserAgent;
$LWP_USER_AGENT = LWP::UserAgent->new;
$LWP_USER_AGENT->env_proxy;
quick note bestie: env_proxy reads proxy settings from your environment variables, which is exactly what I need to do to get thru our firewall. π if you want a different LWP::UserAgent, you can either set it globally with:
XML::Checker::Parser::set_LWP_UserAgent ($my_agent);
or, you can specify it for one specific XML::Checker::Parser by passing it to the constructor:
my $parser = new XML::Checker::Parser (LWP_UserAgent => $my_agent);
currently, LWP gets used when the filename (the one passed to parsefile) starts with one of these URL schemes: http, https, ftp, wais, gopher, or file (followed by a colon). if I missed one, please let me know. π«Ά
the LWP modules are part of libwww-perl, which is available at CPAN.
πΈ External DTDs πΈ
XML::Checker::Parser will try to load and parse external DTDs that are referenced in DOCTYPE definitions β unless you set the SkipExternalDTD option to 1 (the default setting is 0). See CAVEATS for the details on what XML::Checker::Parser does not support. π
XML::Parser (version 2.27 and up) does a much better job at reading external DTDs, because recently external DTD parsing was added to expat. so make sure you set the XML::Parser option ParseParamEnt to 1 and the XML::Checker::Parser option SkipExternalDTD to 1. (they can both be set in the XML::Checker::Parser constructor, no cap.) β
when external DTDs are parsed by XML::Checker::Parser, they are located in this order:
- with the
%URI_MAP, which can be set usingmap_uri. this hash maps external resource ids (like system ID's and public ID's) to full path URI's. it was meant to aid in resolvingPUBLICIDs found inDOCTYPEdeclarations after thePUBLICkeyword, e.g.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
however, you can also use this to force XML::Checker to read DTDs from a different URL than was specified (e.g. from the local file system for performance reasons). β¨
- on the Internet, if their system identifier starts with a protocol (like
http://...) - on the local disk, if their system identifier starts with a slash (absolute path)
- in the
SGML_SEARCH_PATH, if their system identifier is a relative file name. it will use@SGML_SEARCH_PATHif it was set withset_sgml_search_path(), or the colon-separated$ENV{SGML_SEARCH_PATH}, or (if that isn't set) the list(".", "$ENV{'HOME'}/.sgml", "/usr/lib/sgml", "/usr/share/sgml"), which includes the current directory, so it should do the right thing in most cases. π§
π Static methods related to External DTDs π
set_sgml_search_path (dir1, dir2, ...)β external DTDs with relative file paths are looked up using the@SGML_SEARCH_PATH, which you can set with this method. if@SGML_SEARCH_PATHis never set, it will use the colon-separated$ENV{SGML_SEARCH_PATH}instead. if neither are set it uses the list:".","$ENV{'HOME'}/.sgml","/usr/lib/sgml","/usr/share/sgml".
set_sgml_search_path is a static method.
map_uri (pubid => uri, ...)β to define the location ofPUBLICids, as found inDOCTYPEdeclarations after thePUBLICkeyword, e.g.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
call this method, e.g.
XML::Checker::Parser::map_uri (
"-//W3C//DTD HTML 4.0//EN" => "file:/user/html.dtd");
See External DTDs for more info.
XML::Checker::Parser::map_uri is a static method.
π Switching user handlers at parse time π
you should be able to use setHandlers() just as in XML::Parser. (quick honesty moment: using setHandlers has not been tested yet. π)
β Error handling β
XML::Checker::Parser routes the fail handler through XML::Checker::Parser::fail_add_context() before calling your fail handler (i.e. the global fail handler: $XML::Checker::FAIL. See "ERROR_HANDLING" in XML::Checker.) it adds the (line, column, byte) information from XML::Parser to the error context (unless it was the end of the XML document.) π₯
π Supported XML::Parser handlers π
only the following XML::Parser handlers are currently routed through XML::Checker: Init, Final, Char, Start, End, Element, Attlist, Doctype, Unparsed, Notation.
π CAVEATS π
when using XML::Checker::Parser to parse external DTDs (i.e. with SkipExternalDTD => 0), expect trouble when your external DTD contains parameter entities inside declarations or conditional sections. the external DTD should probably have the same encoding as the orignal XML document.
bestie this is a real warning, not a vibe. parameter entities inside declarations or conditional sections = trouble. periodt. π
π«Ά AUTHOR π«Ά
Enno Derksen is the original author.
send bug reports, hints, tips, suggestions to T.J. Mather at <tjmather@tjmather.com>.
π SEE ALSO π
XML::Checker ("SEE_ALSO" in XML::Checker), XML::Parser
and that's the whole page, bestie!! π this one's from perl v5.34.0, dated 2022-06-28, and it was the XML::C...Parser(3pm) page in the User Contributed Perl Documentation. slay, go validate something. π
β¨π¦
π¦ where this page comes from
- debian bookworm: libxml-checker-perl 0.13-7 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libxml-checker-perl 0.13-7 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libxml-checker-perl 0.13-7 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libxml-checker-perl 0.13-7 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libxml-checker-perl 0.13-7 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms