解析报文格式:
<?xml version="1.0" encoding="UTF-8"?>
<InterBOSS>
<Version>0100</Version>
<TestFlag>0</TestFlag>
<BIPType>
<BIPCode>BIPNX001</BIPCode>
<ActivityCode>123456</ActivityCode>
<ActionCode>0</ActionCode>
</BIPType>
<RoutingInfo>
<OrigDomain>BOSS</OrigDomain>
<RouteType>00</RouteType>
<Routing>
<HomeDomain>ZFPT</HomeDomain>
<RouteValue>951</RouteValue>
</Routing>
</RoutingInfo>
<TransInfo>
<SessionID>951190122033358560</SessionID>
<TransIDO>P951T20190122095952S358560</TransIDO>
<TransIDOTime>20190122095952</TransIDOTime>
</TransInfo>
<SvcCont>
<![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<TestReqs>
<serviceNumber>456</serviceNumber>
<Seq>123</Seq>
</TestReqs>]]>
</SvcCont>
接受报文实体类:
报文解析类:
package cn.shijinshi.service;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
/**
* 报文解析
*
* @param <T>
*/
@Service
public class XmlMsgRspService<T> {
/**
* 报文解析 节点SvcCont
* @param xml
* @param clazz
* @return
* @throws Exception
*/
public <T> T parsingXML(String xml,Class<T> clazz) throws Exception {
Constructor<T> constructor = clazz.getConstructor();
T m = constructor.newInstance();
Method[] methods = clazz.getMethods();
Field[] declaredFields = clazz.getDeclaredFields();
Class superclass = clazz.getSuperclass();
Method[] methods1 = superclass.getMethods();
Field[] declaredFields1 = superclass.getDeclaredFields();
Document document = DocumentHelper.parseText(xml);
Element rootElement = document.getRootElement();
Iterator iterator = rootElement.elementIterator();
nodeParsing(m, methods1, rootElement,declaredFields1);
Element e = (Element)rootElement.selectNodes("SvcCont").get(0);
String s = e.getStringValue();
Document document1 = DocumentHelper.parseText(s);
Element rootElement1 = document1.getRootElement();
nodeParsing(m, methods, rootElement1,declaredFields);
return m;
}
public void nodeParsing(Object m , Method[] methods, Element element, Field[] declaredFields) throws InvocationTargetException, IllegalAccessException, InstantiationException {
parsingMsg(m, element, methods, declaredFields);
}
public Object parsingMsg(Object m,Element element,Method[] methods,Field[] declaredFields) throws InvocationTargetException, IllegalAccessException, InstantiationException {
Iterator iteratorSvcCont = element.elementIterator();
while (iteratorSvcCont.hasNext()){
Element stu = (Element) iteratorSvcCont.next();
Iterator<Element> elementIterator = stu.elementIterator();
if("SvcCont".equals(stu.getName().substring(0, 1).toUpperCase() + stu.getName().substring(1))){
continue;
}
if(elementIterator.hasNext()){
childNodes( m, methods, stu, declaredFields);
}else {
setAttribute(m,stu,methods);
}
}
return m;
}
public Object childNodes(Object m,Method[] methods,Element stu,Field[] declaredFields)throws InvocationTargetException, IllegalAccessException, InstantiationException {
for (Field declaredField : declaredFields) {
if(stu.getName().equals(declaredField.getName())){
Object obj = declaredField.getType().newInstance();
Field[] declaredFields1 = declaredField.getType().getDeclaredFields();
Method[] methods1 = declaredField.getType().getMethods();
Object o = parsingMsg(obj, stu, methods1, declaredFields1);
for (Method method : methods) {
if(method.getName().equals("set" + stu.getName().substring(0, 1).toUpperCase() + stu.getName().substring(1))){
String s = (String)method.invoke(m,o);
}
}
}
}
return m;
}
public Object setAttribute(Object m,Element stu,Method[] methods)throws InvocationTargetException, IllegalAccessException{
String stringValue = stu.getStringValue();
for (Method method : methods) {
if(method.getName().equals("set" + stu.getName().substring(0, 1).toUpperCase() + stu.getName().substring(1))){
String s = (String)method.invoke(m,stringValue);
}
}
return m;
}
public boolean cherkBasicDataType(Class clazz){
if(clazz.equals(String.class)||clazz.equals(int.class)||clazz.equals(long.class)||clazz.equals(double.class)
||clazz.equals(char.class)||clazz.equals(byte.class)||clazz.equals(short.class)||clazz.equals(float.class)){
return true;
}
return false;
}
}
测试: