Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 826699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:27:22+00:00 2026-05-15T03:27:22+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:27:22+00:00Added an answer on May 15, 2026 at 3:27 am

    It means that the code is trying to write DTD information outside of where DTD info is legal in XML documents.

    A DTD (Document Type Definition) describes the allowed structure (and possibly content) of the document. I think DTD info must appear before the root node of the document (in the prolog).

    Looking at the code you’ve provided, I think that there are numerous places where the endSection variable is written to the writer. endSection looks like it contains a DTD declaration. Try removing the lines that write endSection after the very first one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a rss feed in my Kohana app. I did this
I'm trying to create a Podcast RSS feed using WCF. The feed seems to
I am trying to create a application that will consume RSS data using .NET
I am trying the RSS Feed code from this website (http://javamix.wordpress.com/category/programs/rss-feed/) It worked fine.
I'm trying to create a gadget for win 7, that retrieves the RSS feed
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
Trying to create a background-image slideshow and am getting this error... This is the
I am trying to publish an Atom/RSS feed in my Java based Google App

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.