java生成xml格式文件-提交sitemap

通过使用org.jdom包的功能生成xml文件,提交搜索资源平台网站sitemap文件:

mavan:



    org.jdom
    jdom
    1.1.3

生成代码:

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public String createXML() throws Exception {
		String xmlurl = "";
		Element urlset = new Element("urlset"); 
		Document document = new Document(urlset);
		//文章链接的集合
		List listStr=new ArrayList();
		for(int i=1;i<200;i++){
			listStr.add("http://www.citywy.com/"+i+".com");
		}
		int i=1;
		for (String str : listStr) {
			System.out.println(str+"生成中..."+i);
			i++;
			//
			Element url = new Element("url");
			//
			Element loc = new Element("loc");
			loc.setText(str);
			url.addContent(loc);
			//
			Element lastmod = new Element("lastmod");
			lastmod.setText("2018-06-06");
			url.addContent(lastmod);
			//
			Element changefreq = new Element("changefreq");
			changefreq.setText("daily");
			url.addContent(changefreq);
			//
			Element priority = new Element("priority");
			priority.setText("0.8");
			url.addContent(priority);
			urlset.addContent(url);
		}
		XMLOutputter XMLOut = new XMLOutputter();  
    	try {
    		Format f = Format.getPrettyFormat();  
    		f.setEncoding("UTF-8");//default=UTF-8
    		XMLOut.setFormat(f); 
    		String path = "D://sitemap.xml";
    		XMLOut.output(document, new FileOutputStream(path));
    	} catch (Exception e) {  
    		e.printStackTrace();  
    	}
		return xmlurl;
	}

在D盘下生成的文件实例:



  
    http://www.citywy.com/1.com
    2018-06-06
    daily
    0.8
  
  
    http://www.citywy.com/2.com
    2018-06-06
    daily
    0.8
  
  
    http://www.citywy.com/3.com
    2018-06-06
    daily
    0.8
  
  ...

你可能感兴趣的:(Java,SEO,JAVA实用小方法)