java - 生成的 XML 中存在意外属性

标签 java xml dom

我正在生成此 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 - 生成的 XML 中存在意外属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28608716/

相关文章:

java - 在 Spring 的配置类中获取经过身份验证的用户

android - 添加 EDITTEXT 作为子菜单选项 android

xml - InnerXml 替换,但仅替换一次

javascript - JavaScript 有没有办法在浏览器解析 HTML 时加载图像路径之前更改图像路径?

javascript - 找出哪些代码导致 DOM 节点创建失控的好方法是什么?

java - 在没有 JdbcOdbc 默认桥的情况下将 Microsoft Access 与 Java 结合使用

java - 防止用户手动下载和使用我的 Java 小程序

Java - 上传到网站 html/js 表单

android - 如何在android中使用inflater将一个xml设计调用到另一个xml设计中

javascript - 如何将焦点移出 IFrame