rome+xslt rss解决方案

rome生成rss文件
private static final String DEFAULT_FEED_TYPE = "default.feed.type"; 
    private static final String FEED_TYPE = "type"; 
    private static final String MIME_TYPE = "application/xml; charset=GB2312"; 
    private static final String COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed"; 
    private static final String DATE_FORMAT = "yyyy-MM-dd"; 
    private String _defaultFeedType; 
    
    public void transferFunds(ArrayList<InfoGenre> genre,ArrayList<InfoPro> pro,String genreid){    
            String fileName="";
            InfoGenre gen=genre.get(0);
            try {
                String feedType = "rss_2.0";//指定rss类型
                
                String value = ""; 
                Properties prop = new Properties(); 
            	String url=this.getClass().getResource("/rss.properties").getPath().trim();
            	url=url.replaceAll("%20"," ");
                InputStream fis = new FileInputStream(url); 
                prop.load(fis); 
                value = prop.getProperty("address"); 
                      
                fileName = value+"//"+genreid+"//"+genreid+".xml";//静态rss存放目录
                String xslpath=value;
                DateFormat dateParser = new SimpleDateFormat(DATE_FORMAT);
  //feed是通过SyndFeedImpl的实例 
                SyndFeed feed = new SyndFeedImpl();
                feed.setFeedType(feedType);
                feed.setTitle(gen.getName());
                feed.setLink(gen.getLink());
                feed.setDescription(gen.getDescription());
                //SyndImage image=feed.getImage();  
                SyndImage image = new SyndImageImpl(); 
                if(image!=null)   {   
	                //image.setLink(gen.getImagelink()); 
	                image.setUrl(gen.getImagelink()); 
	                image.setDescription(gen.getImagedes());   
	                image.setTitle(gen.getImagetitle());   
                }   
                feed.setImage(image);   
  //entries就是item集合
                List<SyndEntry> entries = new ArrayList<SyndEntry>();
  //一个entry就是一个item
                SyndEntry entry=null;
                SyndContent description;
  
  //第一个item
                InfoPro program=null;
                for(int i=0;i<pro.size();i++){
                	program=(InfoPro)pro.get(i);
	                entry = new SyndEntryImpl();
	                entry.setTitle(program.getTitle());
	                entry.setLink(program.getLink());
	                entry.setPublishedDate(dateParser.parse(program.getPublishtime()));
	                description = new SyndContentImpl();
	                description.setType("text/plain");
	                description.setValue(program.getDescription());
	                entry.setDescription(description);
	                entries.add(entry);
                }

  //将entries加入channel
                feed.setEntries(entries);
                File f=new File(value+"//"+genreid);	
         	    if(!f.exists())  f.mkdirs();
                Writer writer = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8");
                SyndFeedOutput output = new SyndFeedOutput();                
                
  //更新客户对应栏目表isupdate字段,提示更新
                if(gen.getIsupdate().equals("1")){
                	boolean flag=updateCusGenre(gen.getId());
                }
                boolean flag1=updateGenre(gen.getId());
  //存盘,形成静态rss
                output.output(feed,writer);              
                writer.close();
                
  //存盘,形成静态html                
                Transform trans=new Transform();
                trans.transForm(genreid, fileName, xslpath);                          
                
            }
            catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("ERROR: "+ex.getMessage());
            }
    }


    rss+xslt转换html输出

public void transForm(String  genreid,String rssPath,String xslPath){
		try {
		   //取得xml和xsl流   
			StreamSource source = new StreamSource(rssPath); 			
			StreamSource style = new StreamSource(xslPath+"//template.xsl"); 
		    
		   //设置输出流,把生成的结果传送到输出流中。构造函数中的输出流也可以是其他形式。
			String value = ""; 
            Properties prop = new Properties(); 
        	String url=this.getClass().getResource("/rss.properties").getPath().trim();
        	url=url.replaceAll("%20"," ");
            InputStream fis = new FileInputStream(url); 
            prop.load(fis); 
            value = prop.getProperty("address");                   
            String fileName = value+"//"+genreid+"//"+genreid+".html";//静态h存放目录         
		    Writer outfile = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8");
		    StreamResult result = new StreamResult(outfile);
		    
		   //初始化转换器  
	       TransformerFactory transFactory = TransformerFactory.newInstance(); 
	       Transformer transformer = transFactory.newTransformer(style);
	       
		   //设置生成html形式结果流的编码为GBK
	       transformer.setOutputProperty(OutputKeys.ENCODING,"GBK");
	       
		   //设置html形式的流是有缩进格式的  
	       transformer.setOutputProperty(OutputKeys.INDENT,"yes");
	       
		  //转换并且输出流   
	       transformer.transform(source,result);
	       
	       }catch(Exception e) { 
	    	   e.printStackTrace(); 
	       }
	}

你可能感兴趣的:(html,xml,F#,XSL)