Go小白,在开发过程中遇到了读取xml的操作,但是xml里的数据是以Excel中Worksheet结构存储的,请问该如何正确提取呢?
↓ xml中的Worksheet ↓
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>zhangyikun</Author>
<LastAuthor>user</LastAuthor>
<Created>2013-10-21T08:50:44Z</Created>
<LastSaved>2013-11-22T02:12:41Z</LastSaved>
<Version>15.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>7785</WindowHeight>
<WindowWidth>18840</WindowWidth>
<WindowTopX>0</WindowTopX>
<WindowTopY>0</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Center"/>
<Borders/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s62" ss:Name="常规 2">
<Alignment ss:Vertical="Center"/>
<Borders/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="9" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s63">
<NumberFormat ss:Format="@"/>
</Style>
<Style ss:ID="s64">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
<NumberFormat ss:Format="@"/>
</Style>
<Style ss:ID="s65">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
</Style>
<Style ss:ID="s67" ss:Parent="s62">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
<Borders/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="9" ss:Color="#000000"/>
<Interior ss:Color="#C0C0C0" ss:Pattern="Solid"/>
<Protection ss:Protected="0"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet">
<Table ss:ExpandedColumnCount="20" ss:ExpandedRowCount="28" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="13.5">
<Column ss:StyleID="s63" ss:Width="68.25"/>
<Column ss:StyleID="s63" ss:Width="45"/>
<Column ss:Width="36" ss:Span="1"/>
<Column ss:Index="5" ss:Width="45"/>
<Column ss:Width="63"/>
<Column ss:Width="45" ss:Span="1"/>
<Column ss:Index="9" ss:Width="36"/>
<Column ss:Width="45"/>
<Column ss:Index="12" ss:Width="45" ss:Span="1"/>
<Column ss:Index="14" ss:Width="63"/>
<Column ss:Width="45"/>
<Column ss:Width="73.5"/>
<Column ss:Width="45" ss:Span="2"/>
<Column ss:Index="20" ss:Width="27"/>
<Row ss:AutoFitHeight="0" ss:StyleID="s65">
<Cell ss:StyleID="s67"><Data ss:Type="String">收货/送检单号</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">是否加急</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">送检人</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">检验员</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">送检时间</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">检验完成时间</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">物料编码</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">物料名称</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">供应商</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">收货数量</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">供应商批次</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">存放位置</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">物料状态</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">检验破坏数量</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">检验数量</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">检验不合格数量</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">检验状态</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">检验结果</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">审核状态</Data></Cell>
<Cell ss:StyleID="s67"><Data ss:Type="String">备注</Data></Cell>
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="String">XXX</Data></Cell>
<Cell><Data ss:Type="String">XXXX</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">EBXXX</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String"> </Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">XXXXX</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String"> </Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">XXXXXXXX</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">XXXX</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">XXX</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">XXXX</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">XXX</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String"> </Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">XXX</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">0</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">0</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">0</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">待检验</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">未检验</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String">未检验</Data></Cell>
<Cell ss:StyleID="s64"><Data ss:Type="String"> </Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Unsynced/>
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>9</PaperSizeIndex>
<HorizontalResolution>1200</HorizontalResolution>
<VerticalResolution>1200</VerticalResolution>
</Print>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>22</ActiveRow>
<ActiveCol>11</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
主要用标准库 encoding/xml 解析 xml
结构体可以使用xml转go结构体进行快速转换
上述代码打印输出了excel中的两行数据,你可以根据自己的需要进行处理,当前在结构体 Workbook中你先可以只定义你想要解析的字段进行提取。
注意: 你提供的xml数据中,第一行的标签<Row> 缺一个 </Row>, 我提供的代码中已经做了补充