Parser
NAME
Sitemap::Parser - Parse existing XML sitemaps
SYNOPSIS
use Sitemap::Parser;
# Parse from file
my @items = Sitemap::Parser.parse-xml-file('sitemap.xml');
for @items -> $item {
say $item.url, ' ', $item.lastmod // 'no date';
}
# Parse from string
my $xml = q:to/XML/;
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/page</loc>
<lastmod>2024-01-01</lastmod>
</url>
</urlset>
XML
my @items2 = Sitemap::Parser.parse-xml-string($xml);
# Parse sitemap index
my @sitemaps = Sitemap::Parser.parse-xml-index-file('sitemap_index.xml');
This module parses raw XML sitemaps only. For automatic format detection and
loose formats (RSS, Atom, HTML, TXT) use the Sitemap::InputParser module.
METHODS
parse-xml-file(IO::Path $file --> List)
Parse an XML sitemap file and return a list of SitemapItem objects.
parse-xml-string(Str $xml --> List)
Parse an XML sitemap string and return a list of SitemapItem objects.
parse-xml-index-file(IO::Path $file --> List)
Parse a sitemap index file and return a list of sitemap URLs.
parse-xml-index-string(Str $xml --> List)
Parse a sitemap index XML string and return a list of sitemap URLs.
parse-xml-files(*@files, :$parallel = True)
Parse multiple sitemap files in parallel (or sequentially if $parallel is False).
Returns a ParseResult with .items (list of Sitemap::Item objects) and .errors (list of failures).
Also supports hash-style access: $result<items>, $result<errors>.
parse-xml-url-recursive(Str $url, :$max-depth, :$verify-ssl)
Recursively parse a sitemap URL. If the URL is a sitemap index, all child sitemaps are fetched
and parsed. Returns a ParseResult with:
.items (all parsed items), .errors, .sitemaps (all sitemap URLs found),
.urls-parsed (count of URLs processed).
Also supports hash-style access: $result<items>, $result<errors>, etc.
parse-xml-file-recursive(IO::Path $file, :$max-depth)
Recursively parse a local sitemap index file and all referenced child sitemaps.
Returns a ParseResult with:
.items, .errors, .sitemaps, .files-parsed (count of files processed).
Also supports hash-style access: $result<items>, $result<errors>, etc.