NanoCMS RSS

Aus Melin DokuWiki

Wechseln zu: Navigation, Suche

Mit Hilfe der Java-Integration in den Templates ist es mit einfachen Mitteln möglich einen RSS-Feed auszulesen und in ein Template umzuwandeln. Damit das Template dabei nicht unübersichtlich wird arbeitet man mit einem Include.

im template schreibt man nur

<%
	<!--CODEINCLUDE:SITE-DIR/cms/themes/wuv09/includes/rss_wuv_leute.java-->>
%>

zusätzlich erzeugt man eine Textdatei msite/cms/themes/wuv09/includes/rss_wuv_leute.java mit dem eigentlichen Java-Code. Dieser Code liest einen RSS-Feed ein (http://www.wuv.de/view/rss/10/1/) und parst diesen mit Hilfe der Java-XML-Funktionen. Der Inhalt wird anschließend formatiert und ausgegeben. Als weitere Optimierung wird parallel in der Hilfsvariable MELIN_MEMORY eine Textversion zusammengebaut, so wird nur ein Durchlauf zur Generierung beider Varianten benötigt.

import java.io.*;
import java.net.*;
import java.net.URL;

import java.util.*;
import java.util.regex.*;
import java.text.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.parsers.*;
import javax.xml.transform.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;

//
// The RSS Libraries, seb@melin.de, 02.06.2010 ==========================
//

public static String getCharacterDataFromElement(Element e, String tagName) { 
	if (e == null) { return null; }
	NodeList name = e.getElementsByTagName(tagName); 
	if (name == null) { return null; }	
	Element line = (Element) name.item(0);
	if (line == null) { return null; }		
	Node child = line.getFirstChild(); 
	// if (true) { return "" + line; }
	if (child == null) { return null; }			
	if (child instanceof CharacterData) { 
		CharacterData cd = (CharacterData) child; 
		return "" + cd.getData();
	}
	return "?";
} 

public static String getParameterDataFromElement(Element e, String tagName, String attr) { 
	if (e == null) { return null; }
	try {
	
	NodeList name = e.getElementsByTagName(tagName); 
	if (name == null) { return null; }	
	Element line = (Element) name.item(0);

	
	if (line.getAttribute(attr) != null) {
		return "" + line.getAttribute(attr);
	}
	} catch (Exception eggy) {}
	return null;
} 

//
// READ RSS FEED AND CONVERT TO HTML - UNTERNEHMEN
// SET 86400 FOR A DAY

String fetchURL   = "http://www.wuv.de/view/rss/10/1/" + 
(int) ((System.currentTimeMillis()/1000)-86400) + "/" + (int) (System.currentTimeMillis()/1000) + "";
String REMOTE_URL = fetchURL;

val = "";
DocumentBuilderFactory _buildFactory = DocumentBuilderFactory.newInstance();				
DocumentBuilder builder = _buildFactory.newDocumentBuilder();
Document doc = builder.parse(REMOTE_URL);

// NodeList list = doc.getDocumentElement().getChildNodes();
org.w3c.dom.NodeList list = doc.getElementsByTagName("item");
int maxlist = list.getLength();
if (maxlist>25) { maxlist = 25; }
int i = 0;

for (i = 0; i < maxlist; i++)
{
	if (i == 0) {
		val += "<table width=\"500\" bgcolor=\"#ffffff\">\n";
		val += "  <tr><td>\n";
		val += "		<img src=\"leute.jpg\" width=\"500\" height=\"20\">\n";
		val += "  </td></tr></table><br>\n";	

		// TEXT AUCH
		  MELIN_MEMORY6 = "--------------------------------------------------------\n";		  		  
		  MELIN_MEMORY6 += "Leute\n";
		  MELIN_MEMORY6 += "------------------------------------------------\n\n";		  
		// TEXT AUCH		
	} 	

	Element element = (Element) list.item(i); 		  
	String description 		= "" + getCharacterDataFromElement(element, "description");
	String link 			= "" + getCharacterDataFromElement(element, "link");
	String title 			= "" + getCharacterDataFromElement(element, "title");
	String image 			= "";
	
	if (getCharacterDataFromElement(element, "description") == null) { description = ""; }
	if (getCharacterDataFromElement(element, "title") == null) 		 { title = ""; }
	if (getCharacterDataFromElement(element, "link") == null) 		 { link = ""; } 	
	 val += "<div style=\"width:500px\"><table width=\"500\" bgcolor=\"#ffffff\"><tr>\n";

	  if ((image != null) && (image.length()>0)) {
		  val += "    	<td valign=top width=121><a href=\""+ link+"\"><IMG SRC=\""+
		  	image+"\" WIDTH=111 align=left border=0 style=\"padding-right:10px; padding-bottom:5px\"></a></td>\n";
		  val += "    <td valign=top width=379>\n";		  	
	  } else {
		  val += "    <td valign=top width=500>\n";	  
	  } 	  
	  description = description.replace("<p>", "");
	  description = description.replace("</p>", "");
	  val += "		<div style=\"font-family: Verdana,Arial,Helvetica,Sans-Serif; \n";
	  val += "			font-weight: bold; color: #21664F; font-size: 13px; line-height: 15px; \"><b>" + title + "</b></div>\n";
	  val += "		<div style=\"font-family: Verdana,Arial,Helvetica,Sans-Serif; font-weight: normal; color: #000000; font-size: 13px; line-height: 14px;\">\n";
	  val += "" + description;
	  val += "			<a href=\""+ link+"\">mehr...</a>\n";
	  val += "		</div>\n";
	  val += "	</td></tr><tr><td> </td></tr></table></div>";

	// TEXT AUCH
	  MELIN_MEMORY6 += title +"\n";
	  MELIN_MEMORY6 += "-----------------------------------------------------\n";		  
	  MELIN_MEMORY6 += description.replaceAll("<strong>", "").replaceAll("</strong>", "")+"\n";
	  MELIN_MEMORY6 += link+"\n\n";
	// TEXT AUCH
   	
} // for