Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1501859
  • 博文数量: 3500
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 43870
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-03 20:31
文章分类

全部博文(3500)

文章存档

2008年(3500)

我的朋友

分类:

2008-05-04 19:41:45

一起学习
 在做WebServices时为了实现复杂数据的传输,我们经常会采用XML格式的字符串来传送,这主要是由于XML具有存取数据,跨平台及跨语言等好处。请看下面例子:

  

public String getAllNewSpace() {
  StringBuffer toClient = new StringBuffer("");
  …………………………
  if (null != dataset) {
  while (dataset.next()) {
  toClient.append("");
  toClient.append("");// toClient.append(dataset.getString(1));
  toClient.append("");//]]
  toClient.append("");
  toClient.append(dataset.getString(2));
  toClient.append("");
  toClient.append("");
  }
  }
  toClient.append("");
  return toClient.toString();
  }

  如果节点中含有“&”,“<”,“>”等字符时,解释此XML时会出错。

  可以有两种解决方法:

  一、 通过Xml Document对象得到XML字符串返回给客户端,不能直接把XmlDocument返回给客户端,因为Java中的Xml Document对象,别的语言是无法正确解释的,我们只能返回XML 字符串:

  

public String getAllNewSpace() {
  Document document = DocumentHelper.createDocument();
  Element root=document.addElement("root");
  …………………………
  if (null != dataset) {
  while (dataset.next()) {
  Element user=root.addElement("User");
  Element userName=user.addElement("UserName");
  userName.setText(dataset.getString(1));
  Element userId=user.addElement("UserId");
  userId.setText (dataset.getString(2));
  }
  }
  return document.asXML();
  }

  二、 给XML 字符串添加DTD验证:在节点值部份添加“public String getAllNewSpace() {

  

StringBuffer toClient = new StringBuffer("");
  …………………………
  if (null != dataset) {
  while (dataset.next()) {
  toClient.append("");
  toClient.append(" toClient.append(dataset.getString(1));
  toClient.append("]]");
  toClient.append("");
  toClient.append(dataset.getString(2));
  toClient.append("");
  toClient.append("");
  }
  }
  toClient.append("");
  return toClient.toString();
  }

TAG: services web

下载本文示例代码


Web Services:处理XML字符串中特殊字符Web Services:处理XML字符串中特殊字符Web Services:处理XML字符串中特殊字符Web Services:处理XML字符串中特殊字符Web Services:处理XML字符串中特殊字符Web Services:处理XML字符串中特殊字符Web Services:处理XML字符串中特殊字符Web Services:处理XML字符串中特殊字符Web Services:处理XML字符串中特殊字符Web Services:处理XML字符串中特殊字符Web Services:处理XML字符串中特殊字符Web Services:处理XML字符串中特殊字符
阅读(122) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~