Java中金蝶凭证xml转wswsvoucher对象

1.引入maven 依赖包


    javax.xml.bind
    jaxb-api
    2.3.1


    com.alibaba.fastjson2
    fastjson2
    2.0.41

    org.projectlombok
    lombok        
    1.18.32

2.改造vo:

        2.1 凭证xml格式如下:

                
                            1100
                            
                                     1100
                                      2024-08-21

                                                ......其他属性省略

                         

                            
                                     1100
                                      2024-08-21

                                                ......其他属性省略

                          

                   

        2.2 对应xml转Java对象格式: 

                2.2.1:     根( )对象:

        import lombok.Data;
        import javax.xml.bind.annotation.*;
        import java.util.List;
        @Data
        @XmlRootElement(name="RESPONSE")
        @XmlAccessorType(XmlAccessType.FIELD)
        public class WSWSVoucherList {
            @XmlElement(name="COMPANYCODE")
            private String COMPANYCODE;
            @XmlElement(name="VOUCHER")
            private List VOUCHERS;
        }

                2.2.2 :对象对应实体类:

                import lombok.Data;
                import javax.xml.bind.annotation.XmlAccessType;
                import javax.xml.bind.annotation.XmlAccessorType;
                import javax.xml.bind.annotation.XmlElement;

                @Data
                @XmlAccessorType(XmlAccessType.FIELD)
                public class WSWSVoucher extends WSBean implements java.io.Serializable                 {
                    @XmlElement(name ="CASHASSTACTTYPE5")
                    private  java.lang.String  cashAsstActType5  ;
                    @XmlElement(name ="OPPACCOUNTSEQ")
                    private int oppAccountSeq ;
                    @XmlElement(name ="CASHASSTACTTYPE4")
                    private java.lang.String cashAsstActType4 ;

                                        ........其他对应属性与xml里对应字段一致,此处省略。按需增加

                                }

3.将xml转换成对应的实体类:

        

        try {
         File file = new File("E:\\凭证.xml");//红色为对应xml的位置和名称

         JAXBContext jaxbContext = JAXBContext.newInstance(WSWSVoucherList.class);
         Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
         StringReader reader = new StringReader(str);
//       Object o=jaxbUnmarshaller.unmarshal(file);
         WSWSVoucherList data1 = (WSWSVoucherList) jaxbUnmarshaller.unmarshal(file);
         System.out.println(data1);
         System.out.println(JSON.toJSONString(data1));
         // Now you can access the data in the 'data' object.
      } catch (Exception e) {
         e.printStackTrace();
      }

                

        

你可能感兴趣的:(xml,java,前端)