我正在尝试使用JAXB创建移动站点地图。根据{{3}},它应具有以下格式:
<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">
<url>
<loc>http://mobile.example.com/article100.html</loc>
<mobile:mobile/>
</url>
</urlset>
为了解决这个问题,我创建了以下bean:
URL.java:
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlType(propOrder = {"loc"})
public class URL {
private String loc;
@XmlElement
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
URLSet.java
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="urlset")
public class URLSet{
private List<URL> urlset;
@XmlAttribute(name="xmlns")
private String xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";
public URLSet() {
urlset = new ArrayList<URL>();
}
@XmlElement(name="url")
public List<URL> getUrlset() {
return urlset;
}
public void setUrlset(List<URL> urlset) {
this.urlset = urlset;
}
}
编组:
List<URL> list = new ArrayList<URL>();
URLSet urlSet = new URLSet();
for(int i = 0 ; i < 5 ; i++) {
URL url = new URL();
url.setLoc("www.test" + i + ".com");
list.add(url);
}
urlSet.setUrlset(list);
JAXBContext jaxbContext = JAXBContext.newInstance(URLSet.class);
Marshaller m = jaxbContext.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
//m.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
//m.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
StringWriter sw = new StringWriter();
m.marshal(urlSet,sw);
log.info("\n" + sw.toString() + "\n");
问题是:如何向每个节点添加静态<mobile:mobile/>
子节点?如果我将它与桌面“sitemap.xml”一起存储,我该如何命名移动站点地图文件?提前谢谢。