Namespace xmlns = Namespace
.getNamespace("http://www.google.com/schemas/sitemap/0.84");
Document document = new Document();
Element urlset = new Element("urlset", xmlns);
Element url = new Element("url", xmlns);
Element loc = new Element("loc", xmlns);
Element lastmod = new Element("lastmod", xmlns);
Element changefreq = new Element("sex", xmlns);
Element priority = new Element("address", xmlns);
loc.addContent("http://www.touzhele.cn");
lastmod.addContent("25");
changefreq.addContent("always");
priority.addContent("1.0");
url.addContent(loc);
url.addContent(lastmod);
url.addContent(changefreq);
url.addContent(priority);
document.setRootElement(urlset);
urlset.addContent(url);
XMLOutputter fmt = new XMLOutputter();
try {
FileWriter writer = new FileWriter("E:/customer.xml");
Format f1 = Format.getPrettyFormat();
f1.setEncoding("UTF-8");
fmt.setFormat(f1);
fmt.output(document, writer);
} catch (IOException e) {
e.printStackTrace();
}
这个代码生成的xml是:<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<url>
<loc>http://www.touzhele.cn</loc>
<lastmod>25</lastmod>
<sex>always</sex>
<address>1.0</address>
</url>
</urlset>
我想生成的xml:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<url>
<loc>http://www.touzhele.cn</loc>
<lastmod>25</lastmod>
<sex>always</sex>
<address>1.0</address>
</url> <url>
<loc>http://www.baidu.cn</loc>
<lastmod>25</lastmod>
<sex>always</sex>
<address>1.0</address>
</url>
</urlset>
请问如何添加多个<url>?
.getNamespace("http://www.google.com/schemas/sitemap/0.84");
Document document = new Document();
Element urlset = new Element("urlset", xmlns);
Element url = new Element("url", xmlns);
Element loc = new Element("loc", xmlns);
Element lastmod = new Element("lastmod", xmlns);
Element changefreq = new Element("sex", xmlns);
Element priority = new Element("address", xmlns);
loc.addContent("http://www.touzhele.cn");
lastmod.addContent("25");
changefreq.addContent("always");
priority.addContent("1.0");
url.addContent(loc);
url.addContent(lastmod);
url.addContent(changefreq);
url.addContent(priority);
document.setRootElement(urlset);
urlset.addContent(url);
XMLOutputter fmt = new XMLOutputter();
try {
FileWriter writer = new FileWriter("E:/customer.xml");
Format f1 = Format.getPrettyFormat();
f1.setEncoding("UTF-8");
fmt.setFormat(f1);
fmt.output(document, writer);
} catch (IOException e) {
e.printStackTrace();
}
这个代码生成的xml是:<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<url>
<loc>http://www.touzhele.cn</loc>
<lastmod>25</lastmod>
<sex>always</sex>
<address>1.0</address>
</url>
</urlset>
我想生成的xml:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<url>
<loc>http://www.touzhele.cn</loc>
<lastmod>25</lastmod>
<sex>always</sex>
<address>1.0</address>
</url> <url>
<loc>http://www.baidu.cn</loc>
<lastmod>25</lastmod>
<sex>always</sex>
<address>1.0</address>
</url>
</urlset>
请问如何添加多个<url>?
url = new Element("url", xmlns);
urlset.addContent(url);
add够了最后再用XMLOutputter fmt = new XMLOutputter();
urlset.addContent(url);多添加一个不就好了啊