Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2788709
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: Java

2014-07-05 13:45:43

把字符串转化为xml(通过dom4j )之后就变成另外一种编码,如果想原样保存到数据库只能通过强制不转码
特别是针对表情的消息体

  1. public class Dom4jTest2222 {
  2.     public static void main(String[] args) throws Exception {
  3.         //String xml = "11\">";
  4.         
  5.         String body="11\" pid=\"73972bed-4def-4b3e-b6f2-0713c3ee2b43\">1700测试文本tt\"/>";
  6.         
  7.         System.out.println(body);
  8.         
  9.         String txt=body.substring(body.indexOf("1700"), body.indexOf("tt\""));
  10.         System.out.println(body);
  11.          txt="<![CDATA["+txt+"]]>";
  12.          body="<bbs type=\"11\"><txt>"+txt+"</txt></bbs>";
  13.         //body=body.replaceFirst("<txt cont", "<txt ");
  14.          //body=body.replaceFirst("tt\"/>", "tt\"/>]]>");
  15.          System.out.println(body);
  16.         Document doc = null;
  17.         try {
  18.             doc = DocumentHelper.parseText(body);
  19.             System.out.println();
  20.         } catch (DocumentException e) {
  21.             e.printStackTrace();
  22.             doc = null;
  23.         }
  24.         
  25.         if (doc != null) {
  26.             Element el =(Element) doc.selectSingleNode("bbs/txt");
  27.             String val=el.getText();
  28.             System.out.println(val);
  29.             
  30.             //el.set
  31.         }
  32.         /*
  33.         if (doc != null) {
  34.             Element el = doc.getRootElement();
  35.             System.out.println("aid="+el.attributeValue("aid"));
  36.             System.out.println("type="+el.attributeValue("type"));
  37.             
  38.             List lst1 = el.elements();
  39.             for (Object node : lst1) {
  40.                 System.out.println("" + ((Element) node).getName());
  41.             }
  42.             List lst2 = doc.selectNodes("bbs/img");
  43.             System.out.println("size=


阅读(1244) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~