Reputation: 3
I'm generating this 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>
I try to delete this attribute: <sitemap xmlns="">
but it's no possible.
What I'm doing wrong?
This is my code to generate 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....");
I try to delete with removeAttribute
method or removeAttNs
but seems not working..
Upvotes: 0
Views: 385
Reputation: 163322
It's not an attribute, it's a namespace declaration. The effect of the namespace declaration is to ensure that your sitemap elements are not in a namespace. It's needed because when you created the sitemap elements, you asked for them to be in no namespace. If you want them in the namespace "http://www.sitemaps.org/schemas/sitemap/0.9", you must create them in that namespace, which you do using createElementNS().
Upvotes: 0
Reputation: 17238
Create your sitemap
, loc
, lastmod
elements in the proper namespace ( you currently create it in the null namespace, which in this case is not the default namespace) :
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");
Upvotes: 2
Reputation: 320
It is a while since i did XML with DOM in Java but if i remember correctly you must set the namespace on all elements, so your dom knows which namespace to use. If it does not know this, it will create an empty XML-Namepsace (short: xmlns) which results in the empty attribute you are seeing.
Upvotes: 0