Error Creating RSS Feed XML file - Java

Posted by GigaPr on Stack Overflow See other posts from Stack Overflow or by GigaPr
Published on 2010-06-02T22:37:56Z Indexed on 2010/06/02 22:44 UTC
Read the original article Hit count: 445

Filed under:
|
|

Hi,

i am trying to create an RssFeed using java this is the class i use

import com.rssFeed.domain.RSS;
import com.rssFeed.domain.RSSItem;
import java.io.FileOutputStream;
import java.util.Iterator;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;



public class RssBuilder {

  private static String XML_BLOCK = "\n";
  private static String XML_INDENT = "\t";

  public static void BuildRss(RSS rss, String xmlfile) throws Exception {

      XMLOutputFactory output = XMLOutputFactory.newInstance();        
      XMLEventWriter writer = output.createXMLEventWriter(new FileOutputStream(xmlfile));
      try
      {                  
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLEvent endSection = eventFactory.createDTD(XML_BLOCK);

        StartDocument startDocument = eventFactory.createStartDocument();
        writer.add(startDocument);
        writer.add(endSection);

        StartElement rssStart = eventFactory.createStartElement("", "", "rss");
        writer.add(rssStart);
        writer.add(eventFactory.createAttribute("version", "2.0"));
        writer.add(endSection);

        writer.add(eventFactory.createStartElement("", "", "channel"));
        writer.add(endSection);

        createNode(writer, "title", rss.getTitle());
        createNode(writer, "description", rss.getDescription());
        createNode(writer, "link", rss.getLink());
        createNode(writer, "dateCreated", rss.getDateCreated().toString());

        createNode(writer, "language", rss.getLanguage());
        createNode(writer, "pubDate", rss.getPubDate().toString());
        createNode(writer, "dateModified", rss.getDateModified().toString());
        createNode(writer, "dateModified", rss.getDateModified().toString());
        createNode(writer, "pubDate", rss.getPubDate().toString());
        createNode(writer, "lastBuildDate", rss.getLastBuildDate().toString());
        createNode(writer, "language", rss.getLanguage().toString());
        createNode(writer, "rating", rss.getRating().toString());

        Iterator<RSSItem> iterator = rss.getRssItems().iterator();
        while (iterator.hasNext()) {
          RSSItem entry = iterator.next();
          writer.add(eventFactory.createStartElement("", "", "item"));
          writer.add(endSection);

          createNode(writer, "title", entry.getTitle());
          createNode(writer, "description", entry.getDescription());
          createNode(writer, "link", entry.getLink());
          createNode(writer, "dateCreated", entry.getDateCreated().toString());
          createNode(writer, "pubDate", entry.getDateModified().toString());
          writer.add(eventFactory.createEndElement("", "", "item"));
          writer.add(endSection);
        }

        writer.add(endSection);
        writer.add(eventFactory.createEndElement("", "", "channel"));
        writer.add(endSection);
        writer.add(eventFactory.createEndElement("", "", "rss"));

        writer.add(endSection);
        writer.add(eventFactory.createEndDocument());
        writer.close();
      }
      catch(Exception e)
      {
          writer.close();
      }
  }

  private static void createNode(XMLEventWriter eventWriter, String name, String value)throws XMLStreamException {

      XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        XMLEvent endSection = eventFactory.createDTD(XML_BLOCK);
        XMLEvent tabSection = eventFactory.createDTD(XML_INDENT);

        StartElement sElement = eventFactory.createStartElement("", "", name);
        eventWriter.add(tabSection);
        eventWriter.add(sElement);

        Characters characters = eventFactory.createCharacters(value);
        eventWriter.add(characters);

        EndElement eElement = eventFactory.createEndElement("", "", name);
        eventWriter.add(eElement);
        eventWriter.add(endSection);
  }

}

But i get the following error

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.xml.stream.XMLStreamException: Can not write DOCTYPE declaration (DTD) when not in prolog any more (state 2; start element(s) written)

root cause

javax.xml.stream.XMLStreamException: Can not write DOCTYPE declaration (DTD) when not in prolog any more (state 2; start element(s) written)

what does it mean?

© Stack Overflow or respective owner

Related posts about java

Related posts about Xml