// Like in reading, we first create the factory objects and the actual writers first
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
FileOutputStream outputStream = new FileOutputStream(feedFile);
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(outputStream);
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
// Now we create the document basic structure and the opening tags
XMLEvent newline = eventFactory.createDTD("\n");
// Create the XML document
StartDocument startDocument = eventFactory.createStartDocument();
eventWriter.add(startDocument);
eventWriter.add(newline);
// Create the rss-tag
StartElement rssBaseElement = eventFactory.createStartElement("", "", "rss");
eventWriter.add(rssBaseElement);
eventWriter.add(eventFactory.createAttribute("version", "2.0"));
eventWriter.add(newline);
// Create the channel-tag
eventWriter.add(eventFactory.createStartElement("", "", "channel"));
eventWriter.add(newline);
// Then we create and write the metadata/header tags
setElementData(eventWriter, "title", feed.getTitle());
setElementData(eventWriter, "description", feed.getDescription());
setElementData(eventWriter, "link", feed.getLink());
setElementData(eventWriter, "language", feed.getLanguage());
setElementData(eventWriter, "copyright", feed.getCopyright());
setElementData(eventWriter, "managingEditor", feed.getManagingEditor());
setElementData(eventWriter, "webmaster", feed.getWebmaster());
setElementData(eventWriter, "pubDate", feed.getPubDate());
setElementData(eventWriter, "lastBuildDate", feed.getLastBuildDate());
setElementData(eventWriter, "category", feed.getCategory());
setElementData(eventWriter, "generator", feed.getGenerator());
setElementData(eventWriter, "ttl", feed.getTtl());
setElementData(eventWriter, "image", feed.getImage());
// And finally we create and write the actual channel/feed items
for (Item item : feed.getItems()) {
//Open element with item tag
eventWriter.add(eventFactory.createStartElement("", "", "item"));
eventWriter.add(newline);
// Add the appropriate child elements
// TODO: Add only the elements that have data
setElementData(eventWriter, "title", item.getTitle());
setElementData(eventWriter, "description", item.getDescription());
setElementData(eventWriter, "link", item.getLink());
setElementData(eventWriter, "author", item.getAuthor());
setElementData(eventWriter, "category", item.getCategory());
setElementData(eventWriter, "enclosure", item.getEnclosure());
setElementData(eventWriter, "guid", item.getGuid());
setElementData(eventWriter, "pubDate", item.getPubDate());
setElementData(eventWriter, "source", item.getSource());
//Close the element
eventWriter.add(eventFactory.createEndElement("", "", "item"));
eventWriter.add(newline);
}
// Close the feed starting tags
eventWriter.add(eventFactory.createEndElement("", "", "channel"));
eventWriter.add(newline);
eventWriter.add(eventFactory.createEndElement("", "", "rss"));
eventWriter.add(newline);
// End the document and close the stream
eventWriter.add(eventFactory.createEndDocument());
eventWriter.close();
}