生成的意外属性 XML

Unexpected attribute in generated XML

我正在生成这个 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 删除,但似乎不起作用..

在适当的命名空间中创建 sitemaploclastmod 元素(您当前在 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 中使用 DOM XML 已经有一段时间了,但如果我没记错的话,你必须在所有元素上设置命名空间,所以你的 dom知道要使用哪个命名空间。如果它不知道这一点,它将创建一个空的 XML-Namepsace(简称:xmlns),这会导致您看到的是空属性。

它不是一个属性,它是一个命名空间声明。命名空间声明的作用是确保您的站点地图元素不在命名空间中。这是必需的,因为当您创建站点地图元素时,您要求它们不在命名空间中。如果您希望它们位于命名空间“http://www.sitemaps.org/schemas/sitemap/0.9”中,则必须在该命名空间中创建它们,您可以使用 createElementNS() 来创建它们。