lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2010年11月21日

根据sitemap.org的schema使用JAXB生成sitemap.xml的读写方法

Filed under: Java,Web — 标签:, , , , , , — lizongbo @ 13:37

根据sitemap.org的schema使用JAXB生成sitemap.xml的读写方法

sitemap协议的中文说明在:
http://sitemaps.org/zh_CN/protocol.php
对应的schema在:
的 XML 格式定义: http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
Sitemap 索引文件格式定义:http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd
向google提交sitemap.xml是在:
https://www.google.com/webmasters/tools/home?hl=zh_CN
向百度提交sitemap.xml是在:http://sitemap.baidu.com/

xsd文件生成java代码的操作步骤如下:
1.下载xsd文件保存到D:\dev\xsds。
2.在JDK的bin目录下运行xjc命令生成java文件:
D:\dev\\jdk1.6.0_22\bin>xjc -d D:\dev\javasrc -p com.lizongbo.web.sitemaps.sitemap D:\dev\xsds\sitemap.xsd
parsing a schema…
compiling a schema…
com\lizongbo\web\sitemaps\sitemap\ObjectFactory.java
com\lizongbo\web\sitemaps\sitemap\TChangeFreq.java
com\lizongbo\web\sitemaps\sitemap\TUrl.java
com\lizongbo\web\sitemaps\sitemap\Urlset.java
com\lizongbo\web\sitemaps\sitemap\package-info.java

D:\dev\Java\jdk1.6.0_22\bin>xjc -d D:\dev\javasrc -p com.lizongbo.web.sitemaps.siteindex D:\dev\xsds\siteindex.xsd
parsing a schema…
compiling a schema…
com\lizongbo\web\sitemaps\siteindex\ObjectFactory.java
com\lizongbo\web\sitemaps\siteindex\Sitemapindex.java
com\lizongbo\web\sitemaps\siteindex\TSitemap.java
com\lizongbo\web\sitemaps\siteindex\package-info.java
3.将java文件都复制到java工程的src目录的对应包中。
4.将xsd文件复制到java工程的com.lizongbo.web.sitemaps包对应的文件夹中。
5.封装工具类SitemapUtil.java,代码如下:
[code]
package com.lizongbo.web.sitemaps;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

import com.lizongbo.web.sitemaps.siteindex.Sitemapindex;
import com.lizongbo.web.sitemaps.siteindex.TSitemap;
import com.lizongbo.web.sitemaps.sitemap.TUrl;
import com.lizongbo.web.sitemaps.sitemap.Urlset;
import com.lizongbo.web.sitemaps.sitemap.ObjectFactory;

/**
* 通过jaxb对sitemap.xml进行生成和解析的工具类
*
* @author lizongbo
*
*/
public class SitemapUtil {
private final static ObjectFactory sitemapFac = new ObjectFactory();
private final static com.lizongbo.web.sitemaps.siteindex.ObjectFactory siteindexFac = new com.lizongbo.web.sitemaps.siteindex.ObjectFactory();
private final static String SITEMAP_SCHEMA_LOCATION = “http://www.sitemaps.org/schemas/sitemap/0.9
+ “http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd”;
private final static String SITEINDEX_SCHEMA_LOCATION = “http://www.sitemaps.org/schemas/sitemap/0.9
+ “http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd”;
private final static String DOCTYPE = “<?xml version=\”1.0\” encoding=\”utf-8\”?>”;
private final static String ProcessingInstruction = “<?xml-stylesheet type=’text/xsl’ href=’http://618119.com/wp-content/plugins/google-sitemap-generator/sitemap.xsl’ ?>”;

public static Sitemapindex createSitemapindex() {
return siteindexFac.createSitemapindex();
}

public static TSitemap createTSitemap() {
return siteindexFac.createTSitemap();
}

public static TUrl createTUrl() {
return sitemapFac.createTUrl();
}

public static Urlset createUrlset() {
return sitemapFac.createUrlset();
}

public static String getSitemapXML(Urlset urlset) throws JAXBException,
SAXException {
JAXBContext jc = JAXBContext
.newInstance(“com.lizongbo.web.sitemaps.sitemap”);
Marshaller m = jc.createMarshaller();
StringWriter sw = new StringWriter();
sw.append(DOCTYPE).append(“\n”);
sw.append(ProcessingInstruction).append(“\n”);
//设置xml文本编码
m.setProperty(Marshaller._ENCODING, “UTF-8”);
//设置为格式化输出结果
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//设置为只输出xml节点片段
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
//指定xsd路径
m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, SITEMAP_SCHEMA_LOCATION);
SchemaFactory sf = SchemaFactory
.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(SitemapUtil.class
.getResource(“sitemap.xsd”));
//绑定Schema进行校验
m.setSchema(schema);
m.marshal(urlset, sw);
return sw.toString();
}

public static Urlset getUrlset(String xml) throws JAXBException,
SAXException {
JAXBContext jc = JAXBContext
.newInstance(“com.lizongbo.web.sitemaps.sitemap”);
Unmarshaller um = jc.createUnmarshaller();
SchemaFactory sf = SchemaFactory
.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(SitemapUtil.class
.getResource(“sitemap.xsd”));
um.setSchema(schema);
return (Urlset) um.unmarshal(new StringReader(xml));
}

public static String getSitemapindexXML(Sitemapindex sitemapindex)
throws JAXBException, SAXException {
JAXBContext jc = JAXBContext
.newInstance(“com.lizongbo.web.sitemaps.siteindex”);
Marshaller m = jc.createMarshaller();
StringWriter sw = new StringWriter();
sw.append(DOCTYPE).append(“\n”);
sw.append(ProcessingInstruction).append(“\n”);
m.setProperty(Marshaller.JAXB_ENCODING, “UTF-8”);
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
SITEINDEX_SCHEMA_LOCATION);
SchemaFactory sf = SchemaFactory
.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(SitemapUtil.class
.getResource(“siteindex.xsd”));
m.setSchema(schema);
m.marshal(sitemapindex, sw);
return sw.toString();
}

public static Sitemapindex getSitemapindex(String xml)
throws JAXBException, SAXException {
JAXBContext jc = JAXBContext
.newInstance(“com.lizongbo.web.sitemaps.siteindex”);
Unmarshaller um = jc.createUnmarshaller();
SchemaFactory sf = SchemaFactory
.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(SitemapUtil.class
.getResource(“siteindex.xsd”));
um.setSchema(schema);
return (Sitemapindex) um.unmarshal(new StringReader(xml));
}

public static void main(String[] args) throws JAXBException, SAXException {
Urlset urlset = SitemapUtil.createUrlset();
TUrl url = SitemapUtil.createTUrl();
url.setLoc(“http://lizongbo.com”);
urlset.getUrl().add(url);
String xml = getSitemapXML(urlset);
System.out.println(xml);
xml = xml.replace(“lizongbo”, “”);
System.out.println(getSitemapXML(getUrlset(xml)));
Sitemapindex sitemapindex = SitemapUtil.createSitemapindex();
TSitemap tSitemap = SitemapUtil.createTSitemap();
tSitemap.setLoc(“http://618119.com/sitemap.xml”);
sitemapindex.getSitemap().add(tSitemap);
xml = getSitemapindexXML(sitemapindex);
System.out.println(xml);
xml = xml.replace(“lizongbo”, “google”);
System.out.println(getSitemapindexXML(getSitemapindex(xml)));
}
}

[/code]

没有评论 »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress