今天在开发sitemap地图时遇到一个小小问题,就是xmlns属性的添加!我们知道sitmap 根结点有一些属性!大体如下:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> </urlset>
xmlns是什么意思,字面意思我们就可以理解,意思就是XML(可扩展标识语言)命名空间!而我们在添加此类属性时一般是用的addAttribute, addNamespce 方法!但实际使用上来看,都不行。
经研究发现原来如此:
Document document = DocumentHelper.createDocument(); Element urlset = document.addElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9"); urlset.addNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance"); urlset.addAttribute("xsi:schemaLocation","http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
博文来源:http://www.jiucool.com/dom4j-xml-xmlns/