Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过JavaSpring构建sitemap.xml_Java_Xml_Spring_Sitemap - Fatal编程技术网

通过JavaSpring构建sitemap.xml

通过JavaSpring构建sitemap.xml,java,xml,spring,sitemap,Java,Xml,Spring,Sitemap,我正在用SpringMVC构建一个sitemap.xml @XmlRootElement(name = "urlset") public class XmlUrlSet{ @XmlElements(@XmlElement(name = "url", type = XmlUrl.class)) private List<XmlUrl> sitemap = new ArrayList<XmlUrl>(); public void addUrl(XmlUrl xmlUrl)

我正在用SpringMVC构建一个sitemap.xml

@XmlRootElement(name = "urlset")
public class XmlUrlSet{
@XmlElements(@XmlElement(name = "url", type = XmlUrl.class))
private List<XmlUrl> sitemap = new ArrayList<XmlUrl>();

public void addUrl(XmlUrl xmlUrl) {
    sitemap.add(xmlUrl);
}

public List<XmlUrl> getXmlUrls() {
    return sitemap;
}
}
@XmlRootElement(name=“urlset”)
公共类XmlUrlSet{
@XmlElements(@XmlElement(name=“url”,type=XmlUrl.class))
私有列表站点地图=新建ArrayList();
public void addUrl(XmlUrl XmlUrl){
添加(xmlUrl);
}
公共列表getXMLURL(){
返回站点地图;
}
}
它呈现如下:

<urlset>
    <url>
        ...
    </url>
    <url>
        ...
    </url>
</urlset>

...
...
我只是想知道如何为xml和xml版本添加名称空间,比如Google的sitemap示例

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <url>
      <loc>http://www.example.com/foo.html</loc> 
   </url>
</urlset>

http://www.example.com/foo.html 

我希望您已经找到了解决方案。不过,这个答案可能对某些人有所帮助

@XmlRootElement(name=“urlset”)
替换为
@XmlRootElement(name=“urlset”,名称空间=”http://www.sitemaps.org/schemas/sitemap/0.9“
将解决您的问题

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
    </url>
</urlset>
更新

如果您尝试过上述解决方案。您将得到如下结果

<ns2:urlset xmlns:ns2="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
    </url>
</ns2:urlset>
它应该能解决你的问题

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
    </url>
</urlset>


看看这个添加包-info.java无法删除名称空间ns2。你知道为什么吗?这个链接救了我。:)@YeWin:这个链接对我来说是无效的,但我不得不将XMLSchema中的Url从
http://www.something.com/something
http://www.sitemaps.org/schemas/sitemap/0.9