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;
 }
}
相關文章
相關標籤/搜索