jsp xml形式的酒店接口查询!!

最近,项目中开发中,遇到需要查询酒店信息这个功能模块!!酒店信息是一个专门的服务商提供的,查询以及接收酒店信息都是以XML形式!!现在,我项目是jsp+Struts+Hibernate 开发的!!我不知道jsp form表单数据如何转换成提供商的那种xml形式继而发送请求!!

发送请求xml:

HotelSearch

1111111111

HBE


请输入申请ID

请输入密码


30
10
1



20070901
20070903

PEK

















接收xml:
<?xml version="1.0" encoding="GB2312" ?>

  • Any
  • -1 ....

java类:
import org.apache.commons.httpclient.*;

import org.apache.commons.httpclient.methods.*;

…………

    try { 

      req = new String(req.getBytes("GB2312"), "ISO8859_1"); 

    } 

    catch (UnsupportedEncodingException ex) { 

      ex.printStackTrace(); 

    }//纠正输入乱码问题,本例中开发环境使用了GB2312编码 

org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();

String strUrl = " http://202.103.129.25/HBE/servlet/tdplogicbookingcontroller";

org.apache.commons.httpclient.methods.PostMethod method = new org.apache.commons.httpclient.methods.PostMethod(strUrl);

method.setParameter("request", req);

org.apache.commons.httpclient.DefaultMethodRetryHandler retryhandler = new org.apache.commons.httpclient.DefaultMethodRetryHandler();

retryhandler.setRequestSentRetryEnabled(false);

retryhandler.setRetryCount(3);

method.setMethodRetryHandler(retryhandler);

client.executeMethod(method);

InputStream is = method.getResponseBodyAsStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(is,"GB2312"));

StringBuffer s = new StringBuffer();

String tmp;

while((tmp=reader.readLine())!=null) {

s.append(tmp);

}

req为以XML形式拼装的请求字符串
不知道jsp里form表单数据如何拼装成xml 存在request里!!

谢谢!!谢谢!!!!

我教你一个简单的办法发送req。
你先定义一个String reqstr="HotelSearch
$$sessingid$$HBE $$officeid$$
$$password$$
...
...
"
自己补充完整,并处理掉回车.
即把里面需要从form里读取的字段,都替换成$$xxx$$类似的标记符号。
然后
String officeid = request.getParameter("officeid");
String password = request.getParameter("password");
...
...
reqstr.replace("$$offcieid$$",officeid).replace("$$password$$",password)......
你明白了么?

你接收到的xml结果,也可以用类似的办法。
xmlstr =" <?xml version="1.0" encoding="GB2312" ?>

  • Any
  • -1 .... "

String code = xmlstr.subString(xmlstr.indexOf(""),xmlstr.indexOf(""));
但这是个笨办法,建议还是用dom4j工具类处理。

要用dom4j来处理和解析xml.现在用jason的比较多,xml的效率太低

你再问清楚一下,酒店信息的服务商,是不是用的web service?那就得用soap协议封装你的request,和接收返回的结果。