2 回答

TA貢獻1878條經(jīng)驗 獲得超4個贊
如果是這種情況,您應(yīng)該查找命名空間定義,這看起來像一個 OpenXML 電子表格。我希望你能找到xmlns="urn:schemas-microsoft-com:office:spreadsheet"和xmlns::ss="urn:schemas-microsoft-com:office:spreadsheet"。
這實際上是同一個命名空間,但 XML 屬性沒有默認命名空間,因此它們需要一個前綴/別名。
有了它,您可以使用 Xpath 表達式從文檔中獲取特定數(shù)據(jù):
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('spreadsheet', 'urn:schemas-microsoft-com:office:spreadsheet');
$records = [];
$rows = $xpath->evaluate('((//spreadsheet:Table)[1]/spreadsheet:Row)[position() > 1]');
foreach ($rows as $row) {
$records[] = [
'Mercado' => $xpath->evaluate('string(spreadsheet:Cell[1])', $row),
'Segmento' => $xpath->evaluate('string(spreadsheet:Cell[2])', $row),
'CodigoDoProjeto' => $xpath->evaluate('string(spreadsheet:Cell[3])', $row)
];
}
var_dump($records);
輸出:
array(3) {
[1]=>
array(3) {
["Mercado"]=>
string(11) "Minera??o"
["Segmento"]=>
string(10) "Portuário"
["CodigoDoProjeto"]=>
string(0) ""
}
[2]=>
array(3) {
["Mercado"]=>
string(10) "Portuário"
["Segmento"]=>
string(0) ""
["CodigoDoProjeto"]=>
string(10) "Greenfield"
}
[3]=>
array(3) {
["Mercado"]=>
string(0) ""
["Segmento"]=>
string(10) "Greenfield"
["CodigoDoProjeto"]=>
string(30) "Large CapEx>>maior que 500MBRL"
}
}
//spreadsheet:Tablefetch any Table,(//spreadsheet:Table)[1]將其限制為第一個,(//spreadsheet:Table)[1]/spreadsheet:Row返回第一個的Row元素Table。
spreadsheet:Cell[1]返回第一個Cell并string(spreadsheet:Cell[1])返回它的文本內(nèi)容。如果它不匹配一個節(jié)點,它將返回一個空字符串。

TA貢獻1796條經(jīng)驗 獲得超10個贊
您可以通過執(zhí)行 $tablas[0] 來僅訪問表數(shù)組中的第一個表?,F(xiàn)在您甚至不需要 foreach 循環(huán)。
<?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++;
}
?>
- 2 回答
- 0 關(guān)注
- 204 瀏覽
添加回答
舉報