我已经使用com.redfin.sitemapgenerator jar创建了站点地图,现在我想在运行时添加节点,即urlset中的url,我已经检查了站点地图是否存在,但是现在我无法追加到已经存在的站点上publicmap 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 "";
}


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

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

最佳答案

    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();
        }
    }

关于java - 将数据追加到已经存在的Sitemap.xml文件中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31802171/

10-10 18:26