【问题标题】:Append data to already existing Sitemap.xml file将数据附加到现有的 Sitemap.xml 文件
【发布时间】:2015-08-04 06:28:54
【问题描述】:

我已经使用 com.redfin.sitemapgenerator jar 创建了站点地图,现在我想添加节点,即运行时 urlset 中的 url,我检查了站点地图是否已经存在但现在我无法附加到已经存在的 sitemap.xml 文件。`public String searchforques() 抛出 IllegalArgumentException { // TODO 自动生成的方法存根 System.out.println("被调用");

     WebSitemapGenerator sitemapGenerator = null;
     WebSitemapUrl sitemapUrl = null;
     File file=new File("C:\\sitemap\\sitemap.xml");
    try {
        if(!file.exists())
        {
        sitemapGenerator = WebSitemapGenerator
            .builder("http://www.testing.com", new File("C:\\sitemap"))
            .gzip(false).build();

        /*------------------------------------------*/
        try {
            sitemapUrl = new WebSitemapUrl.Options(
                "http://www.testing.com/#!dashboard1")
                .lastMod(new Date()).priority(1.0)
                .changeFreq(ChangeFreq.HOURLY).build();
            System.out.println("-----------");
            System.out.println("sitemapUrl :: "+sitemapUrl.getUrl());

             sitemapGenerator.addUrl(sitemapUrl);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /****************/

        }
        else
        {
            System.out.println("file already exist");


            sitemapGenerator = WebSitemapGenerator
            .builder("http://www.testing.com", file)
            .gzip(false).build();

             try {
                    sitemapGenerator
                        .addUrl("http://www.testing.com/#!ques");

                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("sitemapGenerator :: "+sitemapGenerator);
        }


    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

     sitemapGenerator.write();

    // this will configure the URL with lastmod=now, priority=1.0,
    // changefreq=hourly

    // You can add any number of urls here



    return "";
}

但我的 else 部分无法正常工作,它直接导致服务器失败。

我正在使用 gwt 开发 java。 因此,任何人都可以帮助在运行时动态添加 urlset 中的节点(url)。

【问题讨论】:

    标签: java xml gwt sitemap


    【解决方案1】:
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    
    File file=new File("war/abc.xml"); //local war 
    
        String val=null;
    
        if(!file.exists())
        {
            val= "not exist";
            try {
                DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                Document document = docBuilder.newDocument();
    
                Element rootElement = document.createElement("urlset");
                rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
                document.appendChild(rootElement);
    
                Element url = document.createElement("url");
    
                Element loc = document.createElement("loc");
                loc.appendChild(document.createTextNode(urllink));
                url.appendChild(loc);
    
                Element lastmod = document.createElement("lastmod");
                lastmod.appendChild(document.createTextNode(dateText));
                url.appendChild(lastmod);
    
                Element changefreq = document.createElement("changefreq");
                changefreq.appendChild(document.createTextNode("always"));
                url.appendChild(changefreq);
    
                Element priority = document.createElement("priority");
                priority.appendChild(document.createTextNode("1.0"));
                url.appendChild(priority);
    
                rootElement.appendChild(url);
    
                // write the content into xml file
    
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = null;
                try 
                {
                    transformer = transformerFactory.newTransformer();
                } 
                catch (TransformerConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                DOMSource source = new DOMSource(document);
                StreamResult result = new StreamResult(file);
    
                // Output to console for testing
                //StreamResult result = new StreamResult(System.out);
    
                try
                {
                    transformer.transform(source, result);
                } 
                catch (TransformerException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                System.out.println("File saved!");
    
    
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    
        }
        else
        {
            System.out.println("file already exist append ");
            val="file already exist append ";
            DocumentBuilder documentBuilder = null;
    
            try {
                documentBuilder = docFactory.newDocumentBuilder();
            } catch (ParserConfigurationException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
            Document document = null;
            try {
                document = documentBuilder.parse(file);
    
                document.normalize();
            } catch (SAXException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            } catch (IOException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
            Element rootElement = document.getDocumentElement();
    
            Element url = document.createElement("url");
    
            Element loc = document.createElement("loc");
            loc.appendChild(document.createTextNode(urllink));
            url.appendChild(loc);
    
            Element lastmod = document.createElement("lastmod");
            lastmod.appendChild(document.createTextNode(dateText));
            url.appendChild(lastmod);
    
            Element changefreq = document.createElement("changefreq");
            changefreq.appendChild(document.createTextNode("always"));
            url.appendChild(changefreq);
    
            Element priority = document.createElement("priority");
            priority.appendChild(document.createTextNode("1.0"));
            url.appendChild(priority);
    
    
            rootElement.appendChild(url);
    
    
            DOMSource source = new DOMSource(document);
    
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = null;
            try {
                transformer = transformerFactory.newTransformer();
            } catch (TransformerConfigurationException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
    
            StreamResult result = new StreamResult(file);
            System.out.println("result :: "+result.toString());
            try {
                transformer.transform(source, result);
            } catch (TransformerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 2020-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多