我正在生成此 XML:
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap xmlns="" id="0">
<loc>http://x.ae/sitemap-url/ae/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
<sitemap xmlns="" id="1">
<loc>http://x.bh/sitemap-url/bh/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
<sitemap xmlns="" id="2">
<loc>http://x.eg/sitemap-url/eg/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
<sitemap xmlns="" id="3">
<loc>http://x.ma/sitemap-url/ma/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
<sitemap xmlns="" id="4">
<loc>http://x.om/sitemap-url/om/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
<sitemap xmlns="" id="5">
<loc>http://x.qa/sitemap-url/qa/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
<sitemap xmlns="" id="6">
<loc>http://x.tn/sitemap-url/tn/sitemap-index.xml</loc>
<lastmod>2015-02-19</lastmod>
</sitemap>
</sitemapindex>
我尝试删除此属性:<sitemap xmlns="">
但这是不可能的。
我做错了什么?
这是我生成 XML 的代码:
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
//ROOT ELEMENT:
Element rootElement = doc.createElementNS("http://www.sitemaps.org/schemas/sitemap/0.9", "sitemapindex");
doc.appendChild(rootElement);
SortedSet<Country> countries = locationDao.readAllCountriesSorted();
int i =0;
for(Country c : countries){
Element siteMap = doc.createElement("sitemap");
siteMap.setAttribute("id", String.valueOf(i));
//PUT AN ID
Element loc = doc.createElement("loc");
Element lastMod = doc.createElement("lastmod");
loc.appendChild(doc.createTextNode("http://"+c.getDomain()+"/sitemap-url/"+c.getCode().toLowerCase()+"/sitemap-index.xml"));
lastMod.appendChild(doc.createTextNode(DateUtils.getCurrentDateString()));
rootElement.appendChild(siteMap);
siteMap.appendChild(loc);
siteMap.appendChild(lastMod);
i++;
}
;//always appear.
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
//IDENTEM EL RESULTAT
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(doc);
//Si ho volem mostrar en un file:
StreamResult result = new StreamResult(new File("sitemapTest.xml"));
//StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
System.out.println("Document ready....");
我尝试使用 removeAttribute
删除方法或removeAttNs
但似乎不起作用..
最佳答案
在适当的命名空间中创建您的 sitemap
、loc
、lastmod
元素(您当前在 null 命名空间中创建它,在本例中不是默认命名空间):
Element siteMap = doc.createElementNS("http://www.sitemaps.org/schemas/sitemap/0.9", "sitemap");
//...
Element loc = doc.createElementNS("http://www.sitemaps.org/schemas/sitemap/0.9", "loc");
Element lastMod = doc.createElementNS("http://www.sitemaps.org/schemas/sitemap/0.9", "lastmod");
关于java - 生成的 XML 中存在意外属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28608716/