0
votes

I have created sitemap using the com.redfin.sitemapgenerator jar, now I want to add nodes i.e the urls in the urlset at run time, I have checked that the sitemap already exists or not but now I am unable to append to the already existing sitemap.xml file.`public String searchforques() throws IllegalArgumentException { // TODO Auto-generated method stub System.out.println("calledd");

     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 "";
}

But my else part is not working it directly goes to server fail.

I am working on java with gwt. So, any one could help to add dynamically the nodes(urls) in the urlset at runtime.

1

1 Answers

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