开发者

Java based site map generator api [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 7 years ago.

Improve this question

I am looking out f开发者_StackOverflowor a java based site map generator api which I can use to generate site map and target urls. It would be nice if the api is customizable by adding additional code into it.

The site map generator is for the sites that are already hosted. Thanks.

Regards, J


I will say its rather trivial to write your own library as the XML schema is very simple. You can do this with JAXB fragment output.

Below is an some example on how you would write this XML. You'll have to make a Sitemap and Url JAXB POJO but this is rather easy. I probably post the entire code on gist later.

The important thing is that these methods take iterators not lists. Thus you could load from a database the Url objects in chunks instead of other libraries that will load all the objects in memory.

    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;
    } 


Here is one - sitemapgen4j:

http://code.google.com/p/sitemapgen4j/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜