以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Web挖掘技术 』   (http://bbs.xml.org.cn/list.asp?boardid=69)
----  [转帖]Web Services中处理XML字符串中特殊字符  (http://bbs.xml.org.cn/dispbbs.asp?boardid=69&rootid=&id=70457)


--  作者:annesun
--  发布时间:12/14/2008 11:30:00 PM

--  [转帖]Web Services中处理XML字符串中特殊字符
在做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();
  }
转自:[URL=http://awebreport.com/archives/5]Aweb报表[/URL]



--  作者:hexun831012
--  发布时间:12/15/2008 5:49:00 PM

--  
拼字符串不是正确处理XML的方式,你应该寻求一个标准的XmlWriter
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
429.688ms