Scratchpad:Creating XML Using lxml.objectify
From OpenLP
This snippet of code creates a sitemap according to the specs at sitemap.org:
E = objectify.ElementMaker(annotate=False,
namespace=u'http://www.sitemaps.org/schemas/sitemap/0.9',nsmap={None: u'http://www.sitemaps.org/schemas/sitemap/0.9'})
# Create the XMLsite_urls = [u'http://www.domain.com/home.html',u'http://www.domain.com/about.html',u'http://www.domain.com/contact.html',]urls = []
for url in site_urls:
urls.append(E.url(E.loc(url), E.changefreq(u'monthly'), E.priority(u'0.8')))
urlset = E.urlset(*urls)
pretty_xml = etree.tostring(urlset, encoding=u'utf8',
xml_declaration=u'<?xml version="1.0" encoding="UTF-8"?>', pretty_print=True)
sitemap_file = open(config.get(u'sitemap.filename', u'autos/public/sitemap.xml'), u'w+')
sitemap_file.write(pretty_xml)
sitemap_file.close()