Web Services中处理XML字符串中特殊字符

12.08.2008 by admin - 3 Comments - 0
Posted in web相关技术

 在做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();
  }

本文地址:Web Services中处理XML字符串中特殊字符

  1. [...] 相关文章:字符串函数 ; 自定义函数编译报错 ; Web Services中处理XML字符串中特殊字符;用快逸报表轻松实现查询条件的保存 其他相关内容:免费Web报表开发论坛; [...]

  2. [...] 相关文章:字符串函数 ; 自定义函数编译报错 ; Web Services中处理XML字符串中特殊字符;用快逸报表轻松实现查询条件的保存 其他相关内容:免费Web报表开发论坛; [...]

  3. [...] 相关文章:字符串函数 ; 自定义函数编译报错 ; Web Services中处理XML字符串中特殊字符;用快逸报表轻松实现查询条件的保存 其他相关内容:免费Web报表开发论坛; [...]