2 回答
TA贡献1878条经验 获得超4个赞
如果是这种情况,您应该查找命名空间定义,这看起来像一个 OpenXML 电子表格。我希望你能找到xmlns="urn:schemas-microsoft-com:office:spreadsheet"和xmlns::ss="urn:schemas-microsoft-com:office:spreadsheet"。
这实际上是同一个命名空间,但 XML 属性没有默认命名空间,因此它们需要一个前缀/别名。
有了它,您可以使用 Xpath 表达式从文档中获取特定数据:
$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])返回它的文本内容。如果它不匹配一个节点,它将返回一个空字符串。
TA贡献1796条经验 获得超10个赞
您可以通过执行 $tablas[0] 来仅访问表数组中的第一个表。现在您甚至不需要 foreach 循环。
<?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 关注
- 190 浏览
添加回答
举报