[工作代码]dom4j解析实例

工作中,我需要和另一个公司(A公司)共同开发一个模块,我写一个servlet接口,A公司携带xml格式的报文来访问。我采用流的形式读取,在处理后以流的形式写入,在返回(相应)给A公司。

demo:

 package com.sinosoft.undwrt.bl.facade;

 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringBufferInputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException; import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException; import com.sinosoft.sysframework.reference.DBManager;
import com.sinosoft.utiall.schema.PrpDriskSchema;
import com.sinosoft.utility.error.UserException; public class CheckIfPassFacade extends HttpServlet { /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response);
} /**
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
* @author yaowengang
* @time 2016-1-26
* @desc 共享平台访问此servlet,以流的形式返回passFlag(是否审核通过true or false),passTime(审核通过时间),undwrter(审核通过人)
* @desc underwriteflag 1审核通过,9提交到核赔/核保(在双核或共享平台),6注销,
2下发修改,0暂存(在核心) ;
3自动核保(自核单子审核通过后);4无需核保(自核单子审核通过前的标识)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String[] arrs= analyzeXML(request);
//获取指定元素的内容
String taskId= arrs[0];//businessNo
String businessType= arrs[1]; boolean isT= businessType.equals("T")? true: false;
DBManager dbManager = new DBManager();
List list= new ArrayList<String>();
ResultSet resultSet= null;
String underwriteFlag= null;
String passTime= null;
Boolean passFlag= null ;//true:审核通过,false:未审核通过
String undwrter =null;//核保人
BufferedOutputStream ou= null;
try {
if (taskId!=null&&!"".equals(taskId)) {
// boolean isT= taskId.startsWith("T") ? true :false ;//需要改,让平台作为参数传给我
dbManager.open("ddccDataSource");
if (isT) {//投保单
String sqlT= "select underwriteflag,passtime,underwritecode from prptmain where proposalno=?";
dbManager.prepareStatement(sqlT);
dbManager.setString(1, taskId);
resultSet = dbManager.executePreparedQuery();
while (resultSet.next()) {
underwriteFlag= resultSet.getString("underwriteflag");
passTime= resultSet.getString("passtime");
undwrter= resultSet.getString("underwritecode");
passFlag= underwriteFlag.equals("1");
if (!passFlag) {
passTime=null;
undwrter=null;
}
break;
}
/*if (passFlag) {
resultSet= null;
String sql2= "select passtime,underwritecode from prptmain where proposalno=?";
dbManager.prepareStatement(sql2);
dbManager.setString(1, taskId);
resultSet = dbManager.executePreparedQuery();
while (resultSet.next()) {
passTime= resultSet.getString("passtime");
undwrter= resultSet.getString("underwritecode");
break;
}
if (passTime==null) {
passTime="";
}
}*/ }else {//批单
String sqlP= "select underwriteflag,passtime,underwritecode from prpphead where endorseno=?";
dbManager.prepareStatement(sqlP);
dbManager.setString(1, taskId);
resultSet = dbManager.executePreparedQuery();
while (resultSet.next()) {
underwriteFlag= resultSet.getString("underwriteflag");
passTime= resultSet.getString("passtime");
undwrter= resultSet.getString("underwritecode");
passFlag= underwriteFlag.equals("1");
if (!passFlag) {
passTime=null;
undwrter=null;
}
/*passTime= resultSet.getString("passtime");
undwrter= resultSet.getString("underwritecode");*/
/*if (passTime==null) {
passTime="";
}*/
break;
}
/*if (passFlag) {
resultSet= null;
String sql2= "select passtime,underwritecode from prpphead where endorseno=?";
dbManager.prepareStatement(sql2);
dbManager.setString(1, taskId);
resultSet = dbManager.executePreparedQuery();
while (resultSet.next()) {
passTime= resultSet.getString("passtime");
undwrter= resultSet.getString("underwritecode");
break;
}
if (passTime==null) {
passTime="";
}
}*/
} }else {
throw new Exception("解析报文失败!请检查报文是否正确。");
} list.add(""+passFlag);
list.add(passTime);
list.add(undwrter);
String resultXML= assembleXML(list);
System.out.println("给平台返回的xml :"+resultXML);
ou = new BufferedOutputStream(response.getOutputStream());
ou.write(resultXML.getBytes());
ou.flush();
ou.close();
}catch (IOException e1) {
e1.printStackTrace();
}catch (SQLException e2) {
e2.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
if (dbManager!=null) {
try {
dbManager.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} /**
* @desc 将查询所得数据构造成xml格式,便于传送
* @param
* @throws UserException, ParserConfigurationException, FactoryConfigurationError, SAXException,Exception
*/
public String assembleXML(List<String> list) throws UserException, ParserConfigurationException, FactoryConfigurationError, SAXException,Exception {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
strBuilder.append("<ROOT>");
strBuilder.append("<HEAD>");
strBuilder.append("<INFO>");
strBuilder.append("<PASSFLAG>"+ list.get(0)+ "</PASSFLAG>");
strBuilder.append("<PASSTIME>"+ list.get(1)+ "</PASSTIME>");
strBuilder.append("<UNDWRTER>"+ list.get(2)+ "</UNDWRTER>");
strBuilder.append("</INFO>");
strBuilder.append("</HEAD>");
strBuilder.append("</ROOT>");
return strBuilder.toString();
} /**
* @desc dom4j框架解析来自request的报文
* @author yaowengang
* @throws IOException
* @date 20160224
* */
public String[] analyzeXML(HttpServletRequest request) throws IOException {// String[] datas= new String[2];
InputStream XMLInputStream= request.getInputStream();
BufferedReader XMLBuffReader= new BufferedReader(new InputStreamReader(XMLInputStream));
StringBuilder requestXMLSB= new StringBuilder("");
InputStream in= null;
try {
String tempXML;
while (true) {
tempXML= XMLBuffReader.readLine();
if (tempXML==null) {
break;
}
requestXMLSB.append(tempXML);
}
String requestXML= requestXMLSB.toString();
System.out.println("平台发的报文 :"+requestXML);
//解析收到的报文--采用dom4j框架
in= new StringBufferInputStream(requestXML);
SAXReader saxReader= new SAXReader();
Document document= null;
document = saxReader.read(in);
//获取根元素
Element root= document.getRootElement();
Element headElement= root.element("HEAD");
Element infoElement= headElement.element("INFO");
Element businessNoElement= infoElement.element("BUSINESSNO");
Element businessTypeElement= infoElement.element("BUSINESSTYPE");
//获取指定元素的内容
String businessNo= businessNoElement.getText();
String businessType= businessTypeElement.getText();
datas[0]= businessNo;
datas[1]= businessType; }catch (DocumentException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
if (XMLBuffReader!=null) {
XMLBuffReader.close();
}
if (in!=null) {
in.close();
}
}
return datas;
} }
上一篇:C++反射实现(转)


下一篇:(网页)jQueryAJAXtimeout超时问题详解(转)