seo工具

Sitemaps定义:

Google、雅虎、和微软都支持一个被称为xml网站地图(xml Sitemaps)的协议,而百度Sitemap是指百度支持的收录标准,在原有协议上做出了扩展。百度sitemap的作用是通过Sitemap告诉百度蜘蛛全面的站点链接,优化自己的网站。百度Sitemap分为三种格式:txt文本格式、xml格式、Sitemap索引格式。


具体实现,请查看代码注释:

        /**使用本工具请加载jar包org.dom4j
        * 使用方法请看main方法
        * sitemap文件例子可看:
        * https://www.zulily.com/sitemap.xml
        */
        public  class SitemapUtil {
    
        //xml文件头xmlns属性值:例<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        private static String xmlns ="http://www.sitemaps.org/schemas/sitemap/0.9";
        //存放文件路径:可为/aaa/bbb/等,最后位必须带/符号
        private static final String filePath="E:/aa/";
        //private static final String filePath=System.getenv("SITE_MAP_PATH");//可通过环境变量拿
        //生成xml文件的编码
        private static final String encode="gbk";
    
        //以上值可按情况修改
    
        //用于记录子sitemap文件名,最后用于生成主sitemap文件
        private static ArrayList<String> childSitemaps=new ArrayList<String>();
        //先声明节点
        private static Element urlElement;
        private static Element locElement;
        private static Element freqElement;
        private static Element priElement;
    
        /**
         * 使用示例
         * @param args
         */
        public static void main(String[] args){

        //初始化document,并赋予带namespace的根节点
        Document document=new DefaultDocument();
        //xmlns即命名空间
        Element root=document.addElement("urlset",xmlns);
        document.setRootElement(root);

        //写入n个子sitemap文件的示例========================
        boolean isOk=false;
        for (int i=1;i<20;i++) {
            //构建document
            for (int j = 0; j < 50000; j++) {
                createChildDom(root, "https://www.scut.edu.cn/new/gasdvgaswcsfes" + j, "weekly", "0.9");
            }
            //写入文件
             isOk =   SitemapUtil.createChoildSitemap(document, "sitemap-child-"+i+".xml");
            if (!isOk){
                //生成出错
            }
        }
        //=====================================================

       //写入主sitemap
        isOk= SitemapUtil.createMainSitemap();
        if (!isOk){
            //生成出错
        }

    }


    /**
     * 创建主sitemap文件,该方法应该在生成所有子sitemap文件调用。
     */
    public static   boolean  createMainSitemap(){
       Document document=new DefaultDocument();
        Element root=document.addElement("sitemapindex",xmlns);
       for (int i=0;i<childSitemaps.size();i++){
            Element sitemap=root.addElement("sitemap");
            Element loc=sitemap.addElement("loc");
            loc.setText(childSitemaps.get(i));
        }
        try {
            writeDocument("sitemap.xml",document);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            //清空根节点下的所有内容
            if (document.hasContent())
                document.clearContent();
        }
        return true;
    }


    public static   boolean createChoildSitemap(Document document ,String filename){
        try {
            writeDocument(filename,document);
            childSitemaps.add(filename);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            //清空根节点下的所有内容
            if (document.hasContent()&&document.getRootElement().hasContent())
                document.getRootElement().clearContent();
        }
        return true;
    }

    /**
     * 将document写入 filePath目录下的fileName文件里
     * @param fileName
     * @param document
     * @throws IOException
     */
    private static   void writeDocument( String fileName,Document document) throws IOException {
        File file=new File(filePath);
        document.setXMLEncoding(encode);
        recursionMkDir(file);
        OutputStreamWriter outputStreamWriter=new OutputStreamWriter(new FileOutputStream(new File(filePath+fileName)));
        document.write(outputStreamWriter);
        //如果outputStreamWriter未关闭,则关闭outputStreamWriter
        if (outputStreamWriter!=null)
        outputStreamWriter.close();
    }

    /**
     * 创建子sitemap的节点,并将其挂载到root节点
     * 例:<url>
     * <loc>https://www.zulily.com/baby-boy-outerwear</loc>
     * <changefreq>daily</changefreq> 值只能为always, hourly, daily, weekly, monthly, yearly及never
     * <priority>0.4</priority>数值为0到1.0,最高为1.0,一般首页的priority最高为1.0
     * </url>
     * @param root
     * @param url
     * @param changefreq
     * @param priority
     */
    private  static void createChildDom(Element root,String url,String changefreq ,String priority){
        urlElement=root.addElement("url");
        locElement=urlElement.addElement("loc");
        freqElement=urlElement.addElement("changefreq");
        priElement=urlElement.addElement("priority");
        locElement.setText(url);
        freqElement.setText(changefreq);
        priElement.setText(priority);
    }

    //递归创建目录
    private static void recursionMkDir(File file) {
        if (file.getParentFile().exists()) {
            file.mkdir();
        } else {
            recursionMkDir(file.getParentFile());
            file.mkdir();
        }
    }

}

0     1    2     3    4     5    6     7    8     9    10     11    12     13    14     15    16     17    
18     19    20     21    22     23    24     25    26     27    28     29    30     31    32     33    34     35    
36     37    38     39    40     41    42     43    44     45    

<center>Coding Blog     Github Blog </center>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 222,252评论 6 516
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,886评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,814评论 0 361
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,869评论 1 299
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,888评论 6 398
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,475评论 1 312
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,010评论 3 422
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,924评论 0 277
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,469评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,552评论 3 342
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,680评论 1 353
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,362评论 5 351
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,037评论 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,519评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,621评论 1 274
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,099评论 3 378
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,691评论 2 361

推荐阅读更多精彩内容