设置来确定。由于使用的是点对点的消息发送模型,调用con.call()后返回的也是SOAPMessage。
用JAXM开发Web服务(11)
时间:2011-02-07 IBM 陈亚强
对SOAP应答进行处理
接下来对消息进行处理,因为不管查询的结果如何,返回的消息都是例程2所示的结构,故我们使用一个专门的类来把SOAP消息转化成包含有BookVO的Collection,这个类是SOAPToBeanEngine,SOAPToBeanEngine的部分代码如例程16所示。
例程16 SOAPToBeanEngine的部分代码
package com.hellking.webservice;
import javax.xml.messaging.*;
…
public class SOAPToBeanEngine implements DTOEngine
{
SOAPMessage reply;
Collection bookVos;//转换后的结果,用Collection表示
//构造方法,reply为要转换的SOAP消息
public SOAPToBeanEngine(SOAPMessage reply)
{
this.reply=reply;
}
…
public Collection getResult()
{
build();
return bookVos;
}
//build为具体转换的方法
public void build()
{
try
{
Collection ret=new ArrayList();
//System.out.println(reply.getSOAPPart().getEnvelope().getBody().getElementName());
Iterator child=reply.getSOAPPart().getEnvelope().getBody().getChildElements();①
SOAPElement bookall=(SOAPElement)child.next();②
Iterator books=bookall.getChildElements();③
SOAPEnvelope env=reply.getSOAPPart().getEnvelope();
SOAPElement temps;
Name name;
while(books.hasNext())
{
BookVO bookVo=new BookVO();
temps=(SOAPElement)books.next();
String id=(String)(
(temps.getAttributeValue(
(Name)temps.getAllAttributes().next()
)
));
bookVo.setIsbn(id);④
name=env.createName("name","books","http://hellking.webservice.com" );⑤
bookVo.setName(
((SOAPElement)temps.getChildElements(name).next())
.getValue());
//System.out.println(bookVo.getName());
name=env.createName("price" ,"books", "http://hellking.webservice.com" );
bookVo.setPrice(Float.parseFloat(
((SOAPElement)temps.getChildElements(name).next())
.getValue()));
name=env.createName("category", "books", "http://hellking.webservice.com" );
bookVo.setCategory(
((SOAPElement)temps.getChildElements(name).next())
.getValue());
name=env.createName("publisher","books","http://hellking.webservice.com" );
bookVo.setPublisher(
((SOAPElement)temps.getChildElements(name).next())
.getValue());
name=env.createName("description","books","http://hellking.webservice.com" );
bookVo.setDescription(
((SOAPElement)temps.getChildElements(name).next())
.getValue());
// bookVo.setDescription("kdjfkdjfj");
name=env.createName("author","books","http://hellking.webservice.com" );
Collection au=new ArrayList();
Iterator
|