I'm trying to import a .xml table file to mysql using php, it worked fine, but i want to upgrade my code so i can recognize more variations of xml tables. So basically the problem is, i got this code ( just a example, my real table is bigger) of .xml file that I'm trying to read:
...
<Table ss:StyleID="s62">
<Column ss:StyleID="s62"/>
<Column ss:StyleID="s62"/>
<Column ss:StyleID="s62"/>
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s75"><Data ss:Type="String">Mercado</Data></Cell>
<Cell ss:StyleID="s75"><Data ss:Type="String">Segmento</Data></Cell>
<Cell ss:StyleID="s76"><Data ss:Type="String">Codigo do Projeto</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s90"><Data ss:Type="String">Mineração</Data></Cell>
<Cell ss:StyleID="s90"><Data ss:Type="String">Portuário</Data></Cell>
<Cell ss:StyleID="s90"/>
</Row>
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s90"><Data ss:Type="String">Portuário</Data></Cell>
<Cell ss:StyleID="s90"/>
<Cell ss:StyleID="s90"><Data ss:Type="String">Greenfield</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s90"/>
<Cell ss:StyleID="s90"><Data ss:Type="String">Greenfield</Data></Cell>
<Cell ss:StyleID="s90"><Data ss:Type="String">Large CapEx>>maior que 500MBRL</Data></Cell>
</Row>
</Table>
<Worksheet ss:Name="cod">
<Table ss:StyleID="s62">
... ...
</Table>
...
Well, what i want to do is to get the row and data element using getElementByTagName, but i just want to get whats inside the first Table element, not the second, third and so on...
This is what I've tried:
$tabelas = $arquivo->getElementsByTagName("Table");
$rows = $arquivo->getElementsByTagName("Row");
$contRow = 1; (This is just to create a condition to jump the first row)
$contTabelas = TRUE;
foreach ($tabelas as $tabela) {
if ($contTabelas) {
foreach ($rows as $row) {
if ($contRow > 1) {
$Mercado = $row->getElementsByTagName("Data")->item(0)->nodeValue;
$Segmento = $row->getElementsByTagName("Data")->item(1)->nodeValue;
$CodigoDoProjeto = $row->getElementsByTagName("Data")->item(2)->nodeValue;
}
$contRow++;
}
$contTabelas = FALSE;
}
}
It seems that the "foreach($rows as $row)" is getting all the rows from the xml file, but i just want whats inside the "Table" tag. How can I do that??
P.S: I got another problem to solve later, there are a lot of row without an item (Data tag) inside, so i cant get those and the program just jumps to the next one, but i think the solution is just get the 'cell' tag instead 'data'.
You can access just the first table in the tables array by doing $tablas[0]. Now you don't even need a foreach loop.
<?php
$tabelas = $arquivo->getElementsByTagName("Table");
$tablea = $tabelas[0];
$rows = $tablea->getElementsByTagName("Row");
$contRow = 1;
foreach ($rows as $row) {
if ($contRow > 1) {
$Mercado = $row->getElementsByTagName("Data")->item(0)->nodeValue;
$Segmento = $row->getElementsByTagName("Data")->item(1)->nodeValue;
$CodigoDoProjeto = $row->getElementsByTagName("Data")->item(2)->nodeValue;
}
$contRow++;
}
?>