读 XML 文件和 写 XML 文件- 通过JAXB 实现
读 XML 文件
测试文件
test.xml
<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">
<url>
<loc>http://m.example.com/index.html</loc>
<mobile:mobile type="mobile"/>
<lastmod>2009-12-14</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>http://www.example.com/index.html</loc>
<lastmod>2009-12-14</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>http://www.example.com/autoadapt.html</loc>
<mobile:mobile type="pc,mobile"/>
<lastmod>2009-12-14</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>http://www.example.com/htmladapt.html</loc>
<mobile:mobile type="htmladapt"/>
<lastmod>2009-12-14</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
</urlset>
生成对应的 xsd 文件
- 下载 trang.jar (为了方便,博主放到了自己的网盘)
- 生成 xsd 文件
java -jar trang.jar test.xml test.xsd
xsd 文件内容
test.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/" xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9">
<xs:import namespace="http://www.baidu.com/schemas/sitemap-mobile/1/" schemaLocation="mobile.xsd"/>
<xs:element name="urlset">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="