xml 格式消息

场景:发送请求获取用户信息java

传输消息格式: xmltcp

传输消息协议:tcp/ip , http工具

客户端请求xml消息:编码

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<message>
<head>
   <auth>001</auth>
   <sign>abc</sign>
  <version>1</version>
</head>
<body>
   <age>20</age>
</body>
</message

服务端响应xml消息:code

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<message>

<head>
  <recode>0000</recode>
  <reshow>返回成功</reshow>
 <version>1</version>
</head>
<body>
   <list>
     <map>
        <id>1</id>
        <name>john</name>
        <age>20</age>
     </map>
     <map>
        <id>2</id>
        <name>smith</name>
        <age>20</age>
     </map>
   </list>
</body>
</message>

java 请求xml消息生成:xml

 DemoMessage msg = new DemoMessage();
    
    DemoHead head = new DemoHead();
    head.setAuth("001");head.setSign("abc");
    
    DemoBody body = new DemoBody();
    body.setAge("20");
    body.setName("john");
    
    msg.setHead(head);
    msg.setBody(body);
    //生成请求xml字符串
    JAXBUtil util = new JAXBUtil();
    String xmlStr = util.objectToXmlStr(msg, DemoMessage.class);
    
    System.out.println(xmlStr);

java 解析响应xml对象

 DemoRspMessage rspMsg1 = new DemoRspMessage();
 JAXBUtil util = new JAXBUtil();
 rspMsg1 = util.xmlToObj(rspMsg1, xmlRspStr);

java xml字符串,java对象转换工具类ip

public class JAXBUtil {

  @SuppressWarnings("unchecked")
  public String objectToXmlStr(Object obj, Class beanClass) throws Exception {
    JAXBContext context = JAXBContext.newInstance(beanClass);
    // 根据上下文获取marshaller对象
    Marshaller marshaller = context.createMarshaller();
    // 设置编码字符集
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    // 格式化XML输出,有分行和缩进
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshal(obj, baos);
    String xmlObj = new String(baos.toByteArray());
    return xmlObj.replace(" standalone=\"yes\"", "");
  }
  
  /**
   * @Description: 将对象转换为XML而且写入文件
   *
   * @param obj
   * @param beanClass
   * @param file
   * @throws Exception


   */
  @SuppressWarnings("unchecked")
  public void objectToXmlStr(Object obj, Class beanClass, File file) throws Exception {
    JAXBContext context = JAXBContext.newInstance(beanClass);
    // 根据上下文获取marshaller对象
    Marshaller marshaller = context.createMarshaller();
    // 设置编码字符集
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    // 格式化XML输出,有分行和缩进
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    // 打印到控制台
    marshaller.marshal(obj, System.out);
    marshaller.marshal(obj, file);
  }
  
  /**
   * @Description: XML转换为对象
   *
   * @param <T>
   * @param file
   * @param beanClass
   * @return
   * @throws Exception
   */
  @SuppressWarnings("unchecked")
  public <T> T xmlStrToObject(File file, Class<T> beanClass) throws Exception {
    T bean = beanClass.newInstance();
    JAXBContext context = JAXBContext.newInstance(beanClass);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    bean = (T) unmarshaller.unmarshal(file);
    return bean;
  }
  
  /**
  * 
  * @param obj   xml转换后的数据存放的对象
  * @param xml   要转换的xml
  * @return      转换成的对象
  */
 @SuppressWarnings("unchecked")
 public <T> T xmlToObj(T obj,String xml){
   StringReader reader;
   JAXBContext context;
   Unmarshaller unmarshaller;
  reader = new StringReader(xml);
  try {
   context = JAXBContext.newInstance(obj.getClass());
  } catch (JAXBException e) {
   System.out.println("建立转换器环境实例JAXBcontext实例出现异常:"+e.getCause()+"/"+e.getCause());
   return null;
  }
  
  try {
   unmarshaller = context.createUnmarshaller();
  } catch (JAXBException e) {
   System.out.println("建立对象数据转xml数据转换器出现异常:"+e.getCause()+"/"+e.getCause());
   return null;
  }
  
  T retobj=null;
  try {
   retobj = (T) unmarshaller.unmarshal(reader);
  } catch (JAXBException e) {
   System.out.println("xml数据转对象数据出现异常:"+e.getCause()+"/"+e.getCause());
  }
  
  return retobj;
 }
}