快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

将文本格式的文章转变为html/xml格式文本的功能封装到Javabean

在将textarea里面的大段文字储存到数据库,然后提取出来以显示时,格式无效,因为此时是html格式,所以需要转变。

看到JK_10000在javascript区提供了此代码,就顺路牵来。
当然,遇到我这个代码格式化狂人,是肯定要面目全非的,啊哈哈哈哈哈哈......

htmlTextEncoder方式将文本格式转变为html格式。
xmlEncoder方式将文本格式转变为xml格式。


/**
* 字符串编码器类,将字符串转变为指定格式.<br>
* <br>
* 参数字典:<br>
* src - source 来源的简写<br>
* dst - destnation 目的的简写<br>
* fnd - find 查找的简写<br>
* rep - replace 替换的简写<br>
* idx - index 索引,下标的简写<br>
* enc - encoding 编码的简写<br>
* <br>
* 例子:<br>
* <%=ArticleFormat.htmlTextEncoder(yourString)%>
*/
public class StringEncoder
{
/**
* 将字符串src中的子字符串fnd全部替换为新子字符串rep.<br>
* 功能相称于java sdk 1.4的String.replaceAll方式.<br>
* 不同之处在于查找时不是使用正则表达式而是普通字符串.
*/
public static String replaceAll(String src, String fnd, String rep) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;

int idx = dst.indexOf(fnd);

while (idx >= 0)
{
dst = dst.substring(0, idx) + rep + dst.substring(idx + fnd.length(), dst.length());
idx = dst.indexOf(fnd, idx + rep.length());
}

return dst;
}
/**
* 转变为HTML编码.<br>
*/
public static String htmlEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;
dst = replaceAll(dst, "<", "&lt;");
dst = replaceAll(dst, ">", "&rt;");
dst = replaceAll(dst, "/"", "&quot;");
dst = replaceAll(dst, "\\\'", "&#039;");

return dst;
}
/**
* 转变为HTML文字编码.<br>
*/
public static String htmlTextEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;
dst = replaceAll(dst, "<", "&lt;");
dst = replaceAll(dst, ">", "&rt;");
dst = replaceAll(dst, "/"", "&quot;");
dst = replaceAll(dst, "\\\'", "&#039;");
dst = replaceAll(dst, " ", "&nbsp;");
dst = replaceAll(dst, "/r/n", "<br>");
dst = replaceAll(dst, "/r", "<br>");
dst = replaceAll(dst, "/n", "<br>");

return dst;
}
/**
* 转变为URL编码.<br>
*/
public static String urlEncoder(String src, String enc) throws Exception
{
return java.net.URLEncoder.encode(src, enc) ;
}
/**
* 转变为XML编码.<br>
*/
public static String xmlEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;
dst = replaceAll(dst, "&", "&amp;");
dst = replaceAll(dst, "<", "&lt;");
dst = replaceAll(dst, ">", "&gt;");
dst = replaceAll(dst, "/"", "&quot;");
dst = replaceAll(dst, "/\\\'", "&acute;");

return dst;
}
/**
* 转变为SQL编码.<br>
*/
public static String sqlEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

return replaceAll(src, "\\\'", "\\\'\\\'");
}
/**
* 转变为javascript编码.<br>
*/
public static String jsEncoder(String src) throws Exception
{
if (src == null || src.equals(""))
{
return "";
}

String dst = src;
dst = replaceAll(dst, "\\\'", "//\\\'");
dst = replaceAll(dst, "/"", "///"");
//dst = replaceAll(dst, "/r/n", "///n"); // 和/n转变有冲突
dst = replaceAll(dst, "/n", "///n");
dst = replaceAll(dst, "/r", "///n");

return dst;
}
}







返回类别: 教程
上一教程: 为什么Java中继续是有害的
下一教程: Java套接字编程(上)(2)

您可以阅读与"将文本格式的文章转变为html/xml格式文本的功能封装到Javabean"相关的教程:
· 将数据库操作封装到Javabean
· 在JSP中用BEAN封装常用的功能
· 如何判定输入的文本是否日期格式
· JSP结合XML+XSLT将输出转变为HTML格式
· 迅速在应用程序中将文件压缩成ZIP格式文件
    微笑服务 优质保证 索取样品