通过 jackson-xml 来解析 XML 文件

最近有个项目需要解析某商户平台上面导出的账单文件,文件格式如下:

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <?mso-application progid="Excel.Sheet"?>
  3 <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"
  4           xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  5           xmlns:html="http://www.w3.org/TR/REC-html40">
  6     <Worksheet ss:Name="卖出交易1">
  7         <Null/>
  8         <Table ss:ExpandedColumnCount="100" ss:ExpandedRowCount="1000000" ss:DefaultColumnWidth="54.0"
  9                ss:DefaultRowHeight="13.5">
 10             <Null/>
 11             <Row>
 12                 <Cell>
 13                     <Data ss:Type="String">#账号:asdasdasdadasd@gmail.com[XXXXXX]</Data>
 14                 </Cell>
 15             </Row>
 16             <Row>
 17                 <Cell>
 18                     <Data ss:Type="String">#查询起始日期:2018-04-10 00:00:00查询终止日期:2018-04-13 00:00:00</Data>
 19                 </Cell>
 20             </Row>
 21             <Row>
 22                 <Cell>
 23                     <Data ss:Type="String">序号</Data>
 24                 </Cell>
 25                 <Cell>
 26                     <Data ss:Type="String">创建时间</Data>
 27                 </Cell>
 28                 <Cell>
 29                     <Data ss:Type="String">商品名称</Data>
 30                 </Cell>
 31                 <Cell>
 32                     <Data ss:Type="String">商户订单号</Data>
 33                 </Cell>
 34                 <Cell>
 35                     <Data ss:Type="String">XXX交易号</Data>
 36                 </Cell>
 37                 <Cell>
 38                     <Data ss:Type="String">对方账户</Data>
 39                 </Cell>
 40                 <Cell>
 41                     <Data ss:Type="String">对方名称</Data>
 42                 </Cell>
 43                 <Cell>
 44                     <Data ss:Type="String">订单金额(元)</Data>
 45                 </Cell>
 46                 <Cell>
 47                     <Data ss:Type="String">退款金额(元)</Data>
 48                 </Cell>
 49                 <Cell>
 50                     <Data ss:Type="String">服务费(元)</Data>
 51                 </Cell>
 52                 <Cell>
 53                     <Data ss:Type="String">交易状态</Data>
 54                 </Cell>
 55                 <Cell>
 56                     <Data ss:Type="String">退服务费(元)</Data>
 57                 </Cell>
 58                 <Cell>
 59                     <Data ss:Type="String">分公司</Data>
 60                 </Cell>
 61                 <Cell>
 62                     <Data ss:Type="String">银行订单号</Data>
 63                 </Cell>
 64                 <Cell>
 65                     <Data ss:Type="String">交易分类</Data>
 66                 </Cell>
 67                 <Cell>
 68                     <Data ss:Type="String">交易方式</Data>
 69                 </Cell>
 70                 <Cell>
 71                     <Data ss:Type="String">门店</Data>
 72                 </Cell>
 73             </Row>
 74             <Row>
 75                 <Cell>
 76                     <Data ss:Type="String">1</Data>
 77                 </Cell>
 78                 <Cell>
 79                     <Data ss:Type="String">2012-04-12 23:40:05</Data>
 80                 </Cell>
 81                 <Cell>
 82                     <Data ss:Type="String">XXXX</Data>
 83                 </Cell>
 84                 <Cell>
 85                     <Data ss:Type="String">XXX</Data>
 86                 </Cell>
 87                 <Cell>
 88                     <Data ss:Type="String">XXXX</Data>
 89                 </Cell>
 90                 <Cell>
 91                     <Data ss:Type="String">159******90</Data>
 92                 </Cell>
 93                 <Cell>
 94                     <Data ss:Type="String">*黄</Data>
 95                 </Cell>
 96                 <Cell>
 97                     <Data ss:Type="String">58.88</Data>
 98                 </Cell>
 99                 <Cell>
100                     <Data ss:Type="String">0.00</Data>
101                 </Cell>
102                 <Cell>
103                     <Data ss:Type="String">0.12</Data>
104                 </Cell>
105                 <Cell>
106                     <Data ss:Type="String">成功</Data>
107                 </Cell>
108                 <Cell>
109                     <Data ss:Type="String">0.00</Data>
110                 </Cell>
111                 <Cell>
112                     <Data ss:Type="String"></Data>
113                 </Cell>
114                 <Cell>
115                     <Data ss:Type="String"></Data>
116                 </Cell>
117                 <Cell>
118                     <Data ss:Type="String">外部商户</Data>
119                 </Cell>
120                 <Cell>
121                     <Data ss:Type="String">即时到帐</Data>
122                 </Cell>
123                 <Cell>
124                     <Data ss:Type="String"></Data>
125                 </Cell>
126             </Row>
127             <Row>
128                 <Cell>
129                     <Data ss:Type="String">#导出时间:2018-04-13 11:07:31</Data>
130                 </Cell>
131             </Row>
132         </Table>
133     </Worksheet>
134 </Workbook>

第二步,我们创建解析实体的映射类

 1 @Data
 2 public class XXBillExcelDto {
 3 
 4     @JacksonXmlProperty(localName = "Worksheet")
 5     private Worksheet worksheet;
 6 
 7     @Data
 8     public static class Worksheet {
 9         @JacksonXmlElementWrapper(localName = "Table")
10         @JacksonXmlProperty(localName = "Row")
11         private List<Row> rows = new ArrayList<>();
12 
13         public List<Row> getRows() {
14             return rows;
15         }
16 
17         public void setRows(List<Row> rows) {
18             this.rows.addAll(rows);
19         }
20     }
21 
22     @Data
23     public static class Row {
24         @JacksonXmlElementWrapper(localName = "Cell")
25         @JacksonXmlProperty(localName = "Data")
26         private List<String> cells = new ArrayList<>();
27 
28         public List<String> getCells() {
29             return cells;
30         }
31 
32         public void setCells(List<String> cells) {
33             this.cells.addAll(cells);
34         }
35     }
36 }

 

第三步,来看我们的解析方法

1 InputStream is = new FileInputStream(new File("a.xls"));
2         ObjectMapper objectMapper = new XmlMapper();      
3 
4 objectMapper.configure(DeserializationFeate.FAIL_ON_UNKNOWN_PROPERTIES, false);
5 
6 XXBillExcelDto dto = objectMapper.readValue(is, XXBillExcelDto.class);

 

posted @ 2020-04-14 21:13  程序员老郑  阅读(2740)  评论(0编辑  收藏  举报