我正在寻找一个基于java的站点地图生成器api,我可以使用它来生成站点地图和目标URL。如果通过在其中添加额外的代码来定制api,那将是很好的。
站点地图生成器适用于已托管的站点。感谢。
此致 Ĵ
答案 0 :(得分:2)
我会说编写自己的库非常简单,因为XML模式非常简单。您可以使用JAXB片段输出执行此操作。
下面是一个关于如何编写此XML的示例。您必须制作Sitemap和Url JAXB POJO,但这很容易。我可能稍后会在gist上发布整个代码。
重要的是这些方法采用迭代器而不是列表。因此,您可以从数据库加载块中的Url
对象,而不是将加载内存中所有对象的其他库。
protected final static String URLSET_START = "<?xml version='1.0' encoding='UTF-8'?>\n" +
"<urlset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\"\n" +
" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
protected final static String URLSET_END = "\n</urlset>";
protected final static String SITEMAPINDEX_START = "<?xml version='1.0' encoding='UTF-8'?>\n" +
"<sitemapindex xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd\"\n" +
" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
protected final static String SITEMAPINDEX_END = "\n</sitemapindex>";
public static void writeSitemapIndex(Writer writer, Iterator<? extends Sitemap> mappings) throws IOException {
writeXml(writer, SITEMAPINDEX_START, mappings, SITEMAPINDEX_END);
}
public static long writeUrlset(Writer writer, Iterator<Url> urls) throws IOException {
return writeXml(writer, URLSET_START, urls, URLSET_END);
}
private static long writeXml(Writer writer, String start, Iterator<?> it, String end) throws IOException {
writer.write(start);
long count = writeSubtree(writer, it);
writer.write(end);
return count;
}
public static long writeSubtree(Writer writer, Iterator<?> it) throws IOException {
long size = 0;
Marshaller m;
try {
JAXBContext jc = JAXBContext.newInstance(Sitemap.class, Url.class);
m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FRAGMENT, true);
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
} catch (PropertyException e) {
throw new DataBindingException(e);
} catch (JAXBException e) {
throw new DataBindingException(e);
}
boolean first = true;
while (it.hasNext()) {
if (first) first = false;
else writer.write("\n");
try {
m.marshal(it.next(), writer);
} catch (JAXBException e) {
throw new IOException(e);
}
size++;
}
return size;
}
答案 1 :(得分:0)
这是一个 - sitemapgen4j: