我使用com.redfin.sitemapgenerator jar创建了站点地图,现在我想在运行时添加节点,即urlset中的url,我已经检查了站点地图是否已经存在但现在我无法追加到已存在的sitemap.xml文件.public String searchforques() 抛出IllegalArgumentException { // TODO自动生成的方法存根 的System.out.println(" calledd&#34);
WebSitemapGenerator sitemapGenerator = null;
WebSitemapUrl sitemapUrl = null;
File file=new File("C:\\sitemap\\sitemap.xml");
try {
if(!file.exists())
{
sitemapGenerator = WebSitemapGenerator
.builder("http://www.testing.com", new File("C:\\sitemap"))
.gzip(false).build();
/*------------------------------------------*/
try {
sitemapUrl = new WebSitemapUrl.Options(
"http://www.testing.com/#!dashboard1")
.lastMod(new Date()).priority(1.0)
.changeFreq(ChangeFreq.HOURLY).build();
System.out.println("-----------");
System.out.println("sitemapUrl :: "+sitemapUrl.getUrl());
sitemapGenerator.addUrl(sitemapUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/****************/
}
else
{
System.out.println("file already exist");
sitemapGenerator = WebSitemapGenerator
.builder("http://www.testing.com", file)
.gzip(false).build();
try {
sitemapGenerator
.addUrl("http://www.testing.com/#!ques");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("sitemapGenerator :: "+sitemapGenerator);
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sitemapGenerator.write();
// this will configure the URL with lastmod=now, priority=1.0,
// changefreq=hourly
// You can add any number of urls here
return "";
}
但我的其他部分无法正常工作,直接服务器失败。
我正在使用gwt的java。 因此,任何人都可以帮助在运行时动态添加urlset中的节点(url)。
答案 0 :(得分:1)
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
File file=new File("war/abc.xml"); //local war
String val=null;
if(!file.exists())
{
val= "not exist";
try {
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document document = docBuilder.newDocument();
Element rootElement = document.createElement("urlset");
rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
document.appendChild(rootElement);
Element url = document.createElement("url");
Element loc = document.createElement("loc");
loc.appendChild(document.createTextNode(urllink));
url.appendChild(loc);
Element lastmod = document.createElement("lastmod");
lastmod.appendChild(document.createTextNode(dateText));
url.appendChild(lastmod);
Element changefreq = document.createElement("changefreq");
changefreq.appendChild(document.createTextNode("always"));
url.appendChild(changefreq);
Element priority = document.createElement("priority");
priority.appendChild(document.createTextNode("1.0"));
url.appendChild(priority);
rootElement.appendChild(url);
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try
{
transformer = transformerFactory.newTransformer();
}
catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(file);
// Output to console for testing
//StreamResult result = new StreamResult(System.out);
try
{
transformer.transform(source, result);
}
catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("File saved!");
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
System.out.println("file already exist append ");
val="file already exist append ";
DocumentBuilder documentBuilder = null;
try {
documentBuilder = docFactory.newDocumentBuilder();
} catch (ParserConfigurationException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Document document = null;
try {
document = documentBuilder.parse(file);
document.normalize();
} catch (SAXException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Element rootElement = document.getDocumentElement();
Element url = document.createElement("url");
Element loc = document.createElement("loc");
loc.appendChild(document.createTextNode(urllink));
url.appendChild(loc);
Element lastmod = document.createElement("lastmod");
lastmod.appendChild(document.createTextNode(dateText));
url.appendChild(lastmod);
Element changefreq = document.createElement("changefreq");
changefreq.appendChild(document.createTextNode("always"));
url.appendChild(changefreq);
Element priority = document.createElement("priority");
priority.appendChild(document.createTextNode("1.0"));
url.appendChild(priority);
rootElement.appendChild(url);
DOMSource source = new DOMSource(document);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StreamResult result = new StreamResult(file);
System.out.println("result :: "+result.toString());
try {
transformer.transform(source, result);
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}