XML::C...Parser(3pm)

πŸ“¦
version 0.13 Β· 2 versions
  • 0.13 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.13 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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 setting SkipExternalDTD to 1. See External DTDs for the details, iykyk.
  • SkipInsignifWS β€” by default, it will treat insignificant whitespace as regular Char data. but if you set SkipInsignifWS to 1, the user Char handler will not be called when insignificant whitespace shows up. See "INSIGNIFICANT_WHITESPACE" in XML::Checker for the whole story.
  • LWP_UserAgent β€” when you call parsefile() with a URL (instead of a filename) or when loading external DTDs, we use LWP to download the remote file. by default it uses an LWP::UserAgent created 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 using map_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 resolving PUBLIC IDs found in DOCTYPE declarations after the PUBLIC keyword, 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_PATH if it was set with set_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. 🧚

  • 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_PATH is 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 of PUBLIC ids, as found in DOCTYPE declarations after the PUBLIC keyword, 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