dom4j解析xml报文

解析报文格式:

0100 0 BIPNX001 123456 0 BOSS 00 ZFPT 951 951190122033358560 P951T20190122095952S358560 20190122095952 456 123 ]]>

接受报文实体类:

报文解析类:
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
    */
    @Service
    public class XmlMsgRspService {

    /**

    • 报文解析 节点SvcCont

    • @param xml

    • @param clazz

    • @return

    • @throws Exception
      */
      public T parsingXML(String xml,Class clazz) throws Exception {
      Constructor 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 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;
    }

}

测试:

你可能感兴趣的:(dom4j解析xml报文)