CustomXmlHandle.java
4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.tianbo.analysis.handle;
import com.tianbo.analysis.model.CustomReception;
import com.tianbo.analysis.service.*;
import com.tianbo.util.XML.XMLXPath;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXParseException;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;
/**
* @author mrz
* @e
* 海关新舱单回执解析xml文件
*/
@Component
public class CustomXmlHandle{
private static CustomXmlHandle customXmlHandle;
//海关新舱单回执报头
public final static String MessageID = "//Manifest/Head/MessageID";
public final static String FunctionCode = "//Manifest/Head/FunctionCode";
public final static String MessageType = "//Manifest/Head/MessageType";
public final static String SenderID = "//Manifest/Head/SenderID";
public final static String ReceiverID = "//Manifest/Head/ReceiverID";
public final static String SendTime = "//Manifest/Head/SendTime";
public final static String Version = "//Manifest/Head/Version";
//海关新舱单回执报体
public final static String JourneyID = "//Manifest/Response/BorderTransportMeans/JourneyID";
public final static String waybillMaster = "//Manifest/Response/Consignment/TransportContractDocument/ID";
public final static String waybillSecond = "//Manifest/Response/Consignment/AssociatedTransportDocument/ID";
public final static String responseCode = "//Manifest/Response/Consignment/ResponseType/Code";
public final static String errorCode = "//Manifest/Response/ResponseType/Code";
public final static String responseText = "//Manifest/Response/Consignment/ResponseType/Text";
public final static String errorText = "//Manifest/Response/ResponseType/Text";
@Autowired
private CoustomAnalysisService coustomAnalysisService;
//通过@PostConstruct实现初始化bean之前进行的操作,解决service调用空指针问题
@PostConstruct
public void init() {
customXmlHandle = this;
customXmlHandle.coustomAnalysisService = this.coustomAnalysisService;
// 初使化时将已静态化的testService实例化
}
/**
*
* @param file 回执目录的文件
* @return
* @throws DocumentException
* @throws UnsupportedEncodingException
* @throws SAXParseException
*/
public int handelXmlDocument(File file) throws DocumentException, ParseException {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);
String flightNo = "";
String flightDate = "";
//开始解析
String journeyid = XMLXPath.getSingleValueByPath(document,JourneyID);
if(!StringUtils.isEmpty(journeyid)){
if (journeyid.contains("/")){
String[] flightList = journeyid.split("/");
if(flightList.length > 0){
flightNo = flightList[0];
flightDate = flightList[1];
}
}else {
flightNo = journeyid;
}
}
String functionCode = XMLXPath.getSingleValueByPath(document, FunctionCode);
String senderId = XMLXPath.getSingleValueByPath(document, SenderID);
String receiverId = XMLXPath.getSingleValueByPath(document, ReceiverID);
String version = XMLXPath.getSingleValueByPath(document, Version);
String sendTime = XMLXPath.getSingleValueByPath(document, SendTime);
String awbA = XMLXPath.getSingleValueByPath(document,waybillMaster);
//全格式的分单 如 17212345678_ADBD
String awbH = XMLXPath.getSingleValueByPath(document,waybillSecond);
String resCode = XMLXPath.getSingleValueByPath(document,responseCode);
String resText = XMLXPath.getSingleValueByPath(document,responseText);
String messageId = XMLXPath.getSingleValueByPath(document, MessageID);
String messageType = XMLXPath.getSingleValueByPath(document, MessageType);
String errorCodes = XMLXPath.getSingleValueByPath(document, errorCode);
String errorTexts = XMLXPath.getSingleValueByPath(document, errorText);
CustomReception customReception = new CustomReception(messageType, flightNo, flightDate,
awbA, awbH, resText, messageId, senderId, receiverId, sendTime,
version, resCode, functionCode);
return customXmlHandle.coustomAnalysisService.insertRecept(customReception, errorCodes, errorTexts);
}
}