I am wondering if there's a build in component or function to generate the sitemap.xml from CQ? Or the only option is to code one from scratch?
have you looked at
Try to write sling job (more details: http://sling.apache.org/site/eventing-and-jobs.html) and using query builder write the code which builds for you sitemap.xml file in the repository.
Retrieving data ...