当前位置: 代码迷 >> 综合 >> 利用dom4j解析 Google sitemap.xml
  详细解决方案

利用dom4j解析 Google sitemap.xml

热度:6   发布时间:2023-12-10 18:47:04.0

所需 dom4j包
转载出处:中国易成网

package com.xml;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* <p>Title: dom4j 解析XML文件</p>
* <p>Description: 利用dom4j解析 Google sitemap.xml</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: 聚能易成</p>
* Creation date: 2006-4-19
* @author dirboy
* @version 1.0
*/
public class XMLDemo {
 
 public static void main(String[] args) {
  String xmlpath = "c:/sitemap.xml";
  String priority = "0.5";//级 别
  String changefreq = "weekly";//频 率
  String updatetime = getDateTimefileYMD();//更新时间
  try{
   Document document = DocumentHelper.createDocument();  
   Element urlsetElement = document.addElement("urlset");
   urlsetElement.addAttribute("xmlns","http://www.google.com/schemas/sitemap/0.84");
   
   Element urlElement = urlsetElement.addElement("url");
   Element locElement = urlElement.addElement("loc");
   Element lastmodElement = urlElement.addElement("lastmod");
   Element changefreqElement = urlElement.addElement("changefreq");
   Element priorityElement = urlElement.addElement("priority");
   //赋值首页
   locElement.setText("http://www.zaza.com.cn/index.html");
   lastmodElement.setText(updatetime);
   changefreqElement.setText(changefreq);
   priorityElement.setText(priority);
   //END
   for(int i=0;i<10;i++){
    urlElement = urlsetElement.addElement("url");
    locElement = urlElement.addElement("loc");
    lastmodElement = urlElement.addElement("lastmod");
    changefreqElement = urlElement.addElement("changefreq");
    priorityElement = urlElement.addElement("priority");
    //赋值
    locElement.setText("http://www.zaza.com.cn/demo"+i+".html");
    lastmodElement.setText(updatetime);//这里时间是你更新时间,这里暂时统一
    changefreqElement.setText(changefreq);
    priorityElement.setText(priority);     
   }
   XMLWriter writer = new XMLWriter(new FileWriter(new File(xmlpath)));
   writer.write(document);
   writer.close();
   document = null;   
   //格式化
   formatXMLFile(xmlpath,"UTF-8");//   
  }catch(Exception ex){
   ex.printStackTrace();
  } 
 }
 /**
  * 格式化XML文档,并解决中文问题
  * @param xmlpath:xml文件路径
  * @param charSet:格式化的字符集
  * @return
  */
 public static boolean formatXMLFile(String xmlpath,String charSet) {
  boolean returnValue = false;
  try {
   SAXReader saxReader = new SAXReader();
   Document document = saxReader.read(new File(xmlpath));
   XMLWriter output = null;
   OutputFormat format = OutputFormat.createPrettyPrint();
   format.setEncoding(charSet);
   output = new XMLWriter(new FileWriter(new File(xmlpath)), format);
   output.write(document);
   output.close();
   document = null;
   saxReader = null;
   returnValue = true;
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return returnValue;
 }
 public static String getDateTimefileYMD() {
  SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
  String s = simpledateformat.format(new Date());
  return s;
 } 
}

 
  相关解决方案