Skip to navigation
How to create a xml file with python
10.03.15
example: create a google sitemapindex xml: from xml.dom import minidom from xml.dom.minidom import Document import gzip doc = Document() style = doc.createProcessingInstruction('xml-stylesheet','type="text/xsl" href="'+self.style+'"') doc.appendChild(style) base = doc.createElement('sitemapindex') base.setAttribute('xmlns',"http://www.sitemaps.org/schemas/sitemap/0.9") base.setAttribute('xmlns:xsi',"http://www.w3.org/2001/XMLSchema-instance") base.setAttribute('xmlns:xhtml',"http://www.w3.org/1999/xhtml") base.setAttribute('xsi:schemaLocation','http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd') doc.appendChild(base) for i in range(0,100): entry = doc.createElement('sitemap') base.appendChild(entry) loc = doc.createElement('loc') loc_content = doc.createTextNode(self.starturl + '/sitemap/sitemap' + str(i) + '.xml.gz') loc.appendChild(loc_content) entry.appendChild(loc) lastmod = doc.createElement('lastmod') d = datetime.datetime.fromtimestamp(int(time.time())).strftime('%Y-%m-%dT%H:%M:%S+00:00') lastmod_content = doc.createTextNode(d) lastmod.appendChild(lastmod_content) entry.appendChild(lastmod) print("Create Sitemapindex XML ") xml = doc.toxml(encoding='utf-8') with gzip.open(self.sitemapindex, 'wb') as f: f.write(xml)
https://docs.python.org/3.4/library/xml.dom.minidom.html
Reply
Anonymous
Information Epoch 1753107120
Effectiveness beats efficiency.
Home
Notebook
Contact us