XML парсинг?

Статус
В этой теме нельзя размещать новые ответы.

SpaceW

Постоялец
Регистрация
27 Сен 2007
Сообщения
96
Реакции
45
Здравствуйте! Вот такая проблема: есть xml файл, который генерится Метастоком и нужно из него обратотать только один кусок:
Код:
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">Name</Data></Cell>
    <Cell><Data ss:Type="String">Time</Data></Cell>
    <Cell><Data ss:Type="String">Last</Data></Cell>
    <Cell><Data ss:Type="String">Change</Data></Cell>
    <Cell><Data ss:Type="String">PrevClose</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">DAX</Data></Cell>
    <Cell><Data ss:Type="String"> 2009-10-26 12:19:50</Data></Cell>
    <Cell><Data ss:Type="Number">5762.84</Data></Cell>
    <Cell><Data ss:Type="Number">0.39</Data></Cell>
    <Cell><Data ss:Type="Number">5740.25</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">CAC</Data></Cell>
    <Cell><Data ss:Type="String"> 2009-10-26 12:19:45</Data></Cell>
    <Cell><Data ss:Type="Number">3816.82</Data></Cell>
    <Cell><Data ss:Type="Number">0.23</Data></Cell>
    <Cell><Data ss:Type="Number">3808.24</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">DJI</Data></Cell>
    <Cell><Data ss:Type="String"> 2009-10-23</Data></Cell>
    <Cell><Data ss:Type="Number">9972.18</Data></Cell>
    <Cell><Data ss:Type="Number">-1.0825</Data></Cell>
    <Cell><Data ss:Type="Number">9972.18</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">SSEC</Data></Cell>
    <Cell><Data ss:Type="String"> 2009-10-26 10:15:04</Data></Cell>
    <Cell><Data ss:Type="Number">3109.5659999999998</Data></Cell>
    <Cell><Data ss:Type="Number">0.06</Data></Cell>
    <Cell><Data ss:Type="Number">3107.8470000000002</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">BUSP</Data></Cell>
    <Cell><Data ss:Type="String"> 2009-10-23</Data></Cell>
    <Cell><Data ss:Type="Number">65058.84</Data></Cell>
    <Cell><Data ss:Type="Number">-1.6271</Data></Cell>
    <Cell><Data ss:Type="Number">65058.84</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">BSE</Data></Cell>
    <Cell><Data ss:Type="String"> 2009-10-26 12:19:49</Data></Cell>
    <Cell><Data ss:Type="Number">16830.28</Data></Cell>
    <Cell><Data ss:Type="Number">0.12</Data></Cell>
    <Cell><Data ss:Type="Number">16810.810000000001</Data></Cell>
   </Row>
и из этого потом будет создаваться новая табличка вида:

<table>
<tr>
<td>DAX</td>
<td>2009-10-26 12:19:50</td>
<td>5762.84</td>
<td>0.39</td>
<td>5740.25</td>
</tr>
</table>

Но проблема в том, что нет уникальных идентификаторов каких-нибудь, по которым можно было определить, что началась именно этот кусок и когда он кончиться...

Как можно так пропарсить?


Весь код такой:
Код:
<?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>Фирма</Author>
  <LastAuthor>Фирма</LastAuthor>
  <Created>2009-10-26T08:20:29Z</Created>
  <Company>firma</Company>
  <Version>12.00</Version>
 </DocumentProperties>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>7560</WindowHeight>
  <WindowWidth>13485</WindowWidth>
  <WindowTopX>480</WindowTopX>
  <WindowTopY>315</WindowTopY>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Calibri" x:CharSet="204" x:Family="Swiss" ss:Size="11"
    ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Лист1">
  <Table ss:ExpandedColumnCount="5" ss:ExpandedRowCount="7" x:FullColumns="1"
   x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
   <Column ss:Index="2" ss:AutoFitWidth="0" ss:Width="112.5"/>
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">Name</Data></Cell>
    <Cell><Data ss:Type="String">Time</Data></Cell>
    <Cell><Data ss:Type="String">Last</Data></Cell>
    <Cell><Data ss:Type="String">Change</Data></Cell>
    <Cell><Data ss:Type="String">PrevClose</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">DAX</Data></Cell>
    <Cell><Data ss:Type="String"> 2009-10-26 12:19:50</Data></Cell>
    <Cell><Data ss:Type="Number">5762.84</Data></Cell>
    <Cell><Data ss:Type="Number">0.39</Data></Cell>
    <Cell><Data ss:Type="Number">5740.25</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">CAC</Data></Cell>
    <Cell><Data ss:Type="String"> 2009-10-26 12:19:45</Data></Cell>
    <Cell><Data ss:Type="Number">3816.82</Data></Cell>
    <Cell><Data ss:Type="Number">0.23</Data></Cell>
    <Cell><Data ss:Type="Number">3808.24</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">DJI</Data></Cell>
    <Cell><Data ss:Type="String"> 2009-10-23</Data></Cell>
    <Cell><Data ss:Type="Number">9972.18</Data></Cell>
    <Cell><Data ss:Type="Number">-1.0825</Data></Cell>
    <Cell><Data ss:Type="Number">9972.18</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">SSEC</Data></Cell>
    <Cell><Data ss:Type="String"> 2009-10-26 10:15:04</Data></Cell>
    <Cell><Data ss:Type="Number">3109.5659999999998</Data></Cell>
    <Cell><Data ss:Type="Number">0.06</Data></Cell>
    <Cell><Data ss:Type="Number">3107.8470000000002</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">BUSP</Data></Cell>
    <Cell><Data ss:Type="String"> 2009-10-23</Data></Cell>
    <Cell><Data ss:Type="Number">65058.84</Data></Cell>
    <Cell><Data ss:Type="Number">-1.6271</Data></Cell>
    <Cell><Data ss:Type="Number">65058.84</Data></Cell>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell><Data ss:Type="String">BSE</Data></Cell>
    <Cell><Data ss:Type="String"> 2009-10-26 12:19:49</Data></Cell>
    <Cell><Data ss:Type="Number">16830.28</Data></Cell>
    <Cell><Data ss:Type="Number">0.12</Data></Cell>
    <Cell><Data ss:Type="Number">16810.810000000001</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/>
   <Selected/>
   <Panes>
    <Pane>
     <Number>3</Number>
     <RangeSelection>R1C1:R7C5</RangeSelection>
    </Pane>
   </Panes>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
 <Worksheet ss:Name="Лист2">
  <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
   x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
   <Row ss:AutoFitHeight="0"/>
  </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/>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
 <Worksheet ss:Name="Лист3">
  <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
   x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
   <Row ss:AutoFitHeight="0"/>
  </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/>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
</Workbook>
 
вначале вырезаем строчный блок, заключенный в тег Row
PHP:
preg_match_all('/<Row[^>]+ss:AutoFitHeight="\d+">(.*)<\/Row>/Usi', $xml, $out_xml);
после парсим ячейки
PHP:
preg_match_all('/<Cell><Data[^>]+ss:Type="[a-z]+">(.*)<\/Data><\/Cell>/Usi', $out_xml[1][$i], $out_cell);
 
посмотри примеры работы с XPath
 
Статус
В этой теме нельзя размещать новые ответы.
Назад
Сверху