webservice中的xml文件的交互

2009-07-06 795 0

  使用webservice中使用的类型可以是好多种,string、element、document等等甚至可以使对象!

  我使用element来传输xml文件,写了一个文件,源码如下


import java.io.Reader;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
/**

  • 工单查询接口 boos提供serialNumber、serviceNum输入参数
  • @author joypen
  • */
    public class WidebandBusinessQueryService {
    // 提供的查询方法
    public Element queryWideband(Element requestXml) throws Exception {
    Map map = this.databaseMethod(requestXml);
    String responseXml = this.dealResponseXml(map);
    System.out.println("111 "+responseXml);
    Document doc = null;
    Reader strreader=new StringReader(responseXml);
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    doc = builder.parse(new InputSource(strreader));
    return doc.getDocumentElement();
    }
    // 连接数据库,并且执行sql语句
    private Map databaseMethod(Element requestXml) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    Map map = null;
    String serialNumber = null;
    String serviceNum = null;
    try {
    map = this.parseXml(requestXml);
    serialNumber = (String) map.get("SERIALNUMBER");
    serviceNum = (String) map.get("SERVICENUM");
    if(TextUtil.isNull(serialNumber) || "".equals(serialNumber) || TextUtil.isNull(serviceNum) || "".equals(serviceNum)){
    map.put("ERRORCODE", "1");
    map.put("DESCRIPTION", "SERIALNUMBER OR SERVICENUM IS WORING!");
    }else{
    conn = Helper.getDBCnn();
    conn.setAutoCommit(false);
    String sqlString = this.sqlQueryString(serialNumber, serviceNum);
    ps = conn.prepareStatement(sqlString);
    rs = ps.executeQuery();
    map = this.dealResult(rs);
    }
    } catch (Exception e) {
    System.out.print("WidebandBusinessQueryService--数据库连接出错!");
    e.printStackTrace();
    }finally{
    try {
    rs = null;
    ps = null;
    if(conn!=null){
    conn.setAutoCommit(true);
    conn.close();
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    return map;
    }
    // 拼接sql语句
    private String sqlQueryString(String serialNumber, String serviceNum) {
    StringBuffer sql = new StringBuffer();
    sql.append("select * from t_pbb_open_main where SERIALNUMBER = +serialNumber+ and SERVICE_NUM = +serviceNum+ ");
    return sql.toString();
    }
    // 处理返回的结果集
    private Map dealResult(ResultSet rs) {
    HashMap map = null;
    try {
    map = new HashMap();
    if(rs.next()){
    map.put("ERRORCODE", "0");
    map.put("SERIALNUMBER", rs.getString("SERIALNUMBER"));
    map.put("SERVICENUM", rs.getString("SERVICE_NUM"));
    map.put("FORMNO", rs.getString("FORM_NO"));
    map.put("STATE", rs.getString("PROCESS_STATE"));
    map.put("OPERATORNAME", rs.getString("SERVICE_NUM"));
    }else{
    map.put("ERRORCODE", "1");
    map.put("DESCRIPTION", "WITHOUT THIS RECORD!");
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return map;
    }
    // 解析xml,得到serialNumber和serviceNum
    private Map parseXml(Element requestXml) throws Exception {
    Map map = new HashMap();
    String serialNumber="";
    Node tmpNode = XPathAPI.selectSingleNode(requestXml, "//SERIALNUMBER");
    if (tmpNode != null)
    {
    Node txtNode = tmpNode.getFirstChild();
    if (txtNode != null)
    serialNumber = txtNode.getNodeValue();
    }
    String serviceNum="";
    tmpNode = XPathAPI.selectSingleNode(requestXml, "//SERVICENUM");
    if (tmpNode != null)
    {
    Node txtNode = tmpNode.getFirstChild();
    if (txtNode != null)
    serviceNum = txtNode.getNodeValue();
    }
    map.put("SERIALNUMBER", serialNumber);
    map.put("SERVICENUM", serviceNum);
    return map;
    }
    // 拼接responseXml
    private String dealResponseXml(Map responseMap) {
    String resultCode = (String) (TextUtil.isNull(responseMap.get("ERRORCODE")) ? "" : responseMap.get("ERRORCODE"));
    String serialNumber = (String) (TextUtil.isNull(responseMap.get("SERIALNUMBER")) ? "" : responseMap.get("SERIALNUMBER"));
    String serviceNum = (String) (TextUtil.isNull(responseMap.get("SERVICENUM")) ? "" : responseMap.get("SERVICENUM"));
    String formNo = (String) (TextUtil.isNull(responseMap.get("FORMNO")) ? "" : responseMap.get("FORMNO"));
    String description = (String) (TextUtil.isNull(responseMap.get("DESCRIPTION")) ? "" : responseMap.get("DESCRIPTION"));
    String time = (String) (TextUtil.isNull(responseMap.get("time")) ? "" : responseMap.get("time"));
    String state = (String) (TextUtil.isNull(responseMap.get("STATE")) ? "" : responseMap.get("STATE"));
    String operatorName = (String) (TextUtil.isNull(responseMap.get("OPERATORNAME")) ? "" : responseMap.get("OPERATORNAME"));
    String operatorPhone = (String) (TextUtil.isNull(responseMap.get("OPERATORPHONE")) ? "" : responseMap.get("OPERATORPHONE"));
    StringBuffer sb = new StringBuffer();
    sb.append("

");
sb.append("
");
sb.append(""+resultCode+"
");
sb.append(""+serialNumber+"
");
sb.append(""+serviceNum+"
");
sb.append(""+formNo+"
");
sb.append("响应时间
");
sb.append("失败原因
");
sb.append("
");
sb.append("当前环节
");
sb.append("操作人姓名
");
sb.append("操作人电话
");
sb.append("
");
sb.append("
");
return sb.toString();
}
public static void main(String[] args) throws Exception {
String reqeustXml = "321312陶宏辉请求时间";
Document doc = null;
Reader strreader=new StringReader(reqeustXml);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
doc = builder.parse(new InputSource(strreader));
Element responseXml = new WidebandBusinessQueryService().queryWideband(doc.getDocumentElement());
String testXml = "";
Node tempNode = XPathAPI.selectSingleNode(responseXml, "//OPERATORNAME");
if(tempNode!=null){
Node txtNode = tempNode.getFirstChild();
if(txtNode!=null){
testXml = txtNode.getTextContent();
}
}
System.out.println("reqeust: "+reqeustXml);
System.out.println("testXml: "+testXml);
}
}

相关文章

15年来的手艺之路:手艺人赵鹏的自述
纪念 Google 25 周年:从搜索引擎到科技巨头的演变之路
1小时编写一个支持七牛上传的 markdown 客户端3(打包发布篇)
1小时编写一个支持七牛上传的 markdown 客户端2(代码优化篇)
1小时编写一个支持七牛上传的 markdown 客户端1(技术实现篇)
从 wordpress 转移到 hexo

发布评论