webservice--解析xml 客户端请求的xml格式:
<?xml version="1.0" encoding="utf-8" ?>
<selectPage>
<start></start>
<end></end>
</selectPage>
服务器响应的xml格式:
<?xml version="1.0" encoding="utf-8" ?>
  <selectPage>
    <accountName></accountName>
</selectPage>
服务端
实体:
package l.entity;
import java.io.Serializable;
public class Account implements Serializable {
private Integer accountNo;
private String accountName;
private String password;
private Integer balance;
//private java.util.Date openDate;
public Integer getAccountNo() {
return accountNo;
}
public void setAccountNo(Integer accountNo) {
this.accountNo = accountNo;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getBalance() {
return balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
使用dom4j解析xml文件
public Account(Integer accountNo, String accountName, String password,
Integer balance) {
super();
this.accountNo = accountNo;
this.accountName = accountName;
this.password = password;
this.balance = balance;
}
public Account() {
super();
}
@Override
public String toString() {
return "Account [accountNo=" + accountNo + ", accountName="                + accountName + ", password=" + password + ", balance="                + balance + "]";
}
}
实现类:
package l.serivce;
import java.util.List;
import javax.jws.WebService;
l.ws.WebFault;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import l.dao.AccountDao;
import l.dao.AccountDaoImpl;
import l.entity.Account;
/**
*
* 实现类:解析客户端传来的String数据,发送xml数据给客户端
*
*/
@WebService
public class AccountServiceImpl implements AccountService { private AccountDao ad = new AccountDaoImpl();
@Override
public String selectPage(String info) throws Exception {
//解析请求的xml
Account account = parseXml(info);
int start = AccountNo()-1;  //起始下标
int end = Balance()-start;  //记录数
//查询区域列表
List<Account> list = ad.selectPage(start, end);
//解析list响应为客户端
String psrseList = psrseList(list);
return psrseList;
}
//解析请求的xml
private Account parseXml(String info) throws Exception{
Account account = new Account();
//使⽤dom4j解析
Document document=null;
try {
document = DocumentHelper.parseText(info);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("出错了");
}
String start = document.selectSingleNode("selectPage/start").getText();
String end = document.selectSingleNode("selectPage/end").getText();
account.setAccountNo(Integer.parseInt(start));
account.setBalance(Integer.parseInt(end));
return account;
}
//解析list,响应回去
private String psrseList(List<Account> list){
Document document = ateDocument();
Element root = ateElement("areas");
//添加根节点
document.add(root);
for (Account account : list) {
String accountName = AccountName();
root.addElement("accountName").addText(accountName);
}
return document.asXML();
}
}
客户端:
package client;
import java.URL;
import java.util.ArrayList;
import java.util.List;
l.namespace.QName;
l.ws.Service;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
import l.entity.Account;
import l.serivce.AccountServiceImpl;
/**
* 使⽤jaxws标准⽅法,向服务端发送String数据,接收服务器响应的String数据
*
*/
public class MyClient_local {
public static void main(String[] args) throws Exception {
URL wsdlDocumentLocation = new URL("127.0.0.1:12345/area?wsdl");
//从wsdl中到服务视图
QName serviceName = new QName("l.zpark/","AccountServiceImplService");
/
/创建服务视图
Service ate(wsdlDocumentLocation , serviceName);
//1.从服务视图中得到portType对象
AccountServiceImpl weatherWebServiceSoap = Port(AccountServiceImpl.class);
//调⽤portType⽅法,发送xml数据
String request =getXml(1,3);
String query = weatherWebServiceSoap.selectPage(request);
//解析服务端传来的数据
List<Account> parseXml = parseXml(query);
for (Account account : parseXml) {
System.out.AccountName());
}
}
//解析服务端的xml,封装成对象
private static List<Account> parseXml(String info) throws Exception{                System.out.println(info);
List<Account> list = new ArrayList<Account>();
//使⽤dom4j解析
Document document = DocumentHelper.parseText(info);
List selectNodes=null;
try {
selectNodes = document.selectNodes("/areas//accountName");                } catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("出错了");
}
//遍历根节点
for (Object object : selectNodes) {
Node accountName = (Node)object;
String text = Text();
Account account = new Account();
account.setAccountName(text);
list.add(account);
}
return list;
}
//发送xml,根据接⼝描述组织内容
private static String getXml(int start,int end){
String Xml="<?xml version=\"1.0\"  encoding=\"utf-8\" ?>"+
"<selectPage>"+
"<start>"+start+"</start>" +
"<end>"+end+"</end>"+
"</selectPage>";
return Xml;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。