Python生成XML,如何添加<mobile:mobile type="mobile"/>这种标签

<?xml version="1.0" encoding="UTF-8" ?> 
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/"> 
<url> 
<loc>http://m.example.com/index.html</loc> 
<mobile:mobile type="mobile"/>
<lastmod>2009-12-14</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url> 
</urlset>
xml.startElement('mobile:mobile', {'type':'pc,mobile'})

这行代码添加出来的不带/结尾,xml解析会出错

阅读 5.8k
1 个回答

看上去和我之前写mobile的sitemap好像,似乎不需要type="mobile"吧

https://github.com/phodal/phodaldev/blob/master/templates/sitemap_mobile.xml

XML里面必须要以/结尾

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">
{% spaceless %}
{% for url in urlset %}
  <url>
    <loc>{{ url.location }}</loc>
    {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %}
    {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
    {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
    <mobile:mobile/>
   </url>
{% endfor %}
{% endspaceless %}
</urlset>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题