【发布时间】:2016-07-20 07:04:27
【问题描述】:
我有一个试图用 PHP 解析的 XML 文件(如下)。
<content>
<row label='DEV'>
<cell href="exUrl('URL')" status='0'>12345</cell>
<cell href="exUrl('URL')" status='1'>12345</cell>
<cell href="exUrl('URL')" status='1'>12345</cell>
<cell href="exUrl('URL')" status='1'>12345</cell>
<cell href="exUrl('URL')" status='1'>12345</cell>
</row>
<row label='DEV2'>
<cell href="exUrl('URL')" status='1'>56789</cell>
<cell href="exUrl('URL')" status='1'>56789</cell>
<cell href="exUrl('URL')" status='1'>56789</cell>
<cell href="exUrl('URL')" status='1'>56789</cell>
<cell href="exUrl('URL')" status='0'>56789</cell>
</row>
</content>
我目前正在使用 PHP 对 XML 文档中的多个“行”求和(下面的示例)。
$dom = new DOMDocument();
$html = $dom->loadHTMLFile("XML.xml");
$dom->preserveWhiteSpace = false;
$tables = $dom->getElementsByTagName('content');
$rows = $tables->item(0)->getElementsByTagName('row');
foreach ($rows as $row)
{
$cols = $row->getElementsByTagName('cell');
$totalValues += $cols->item(4)->nodeValue;
}
我已经更新了 for 循环以包含一个 if 语句来检查状态值,但这似乎不起作用。
foreach ($rows as $row)
{
$cols = $row->getElementsByTagName('cell');
$totalValues += $cols->item(4)->nodeValue;
if(($cols->item(4)->getElementsByTagName('status')->nodeValue) == 0) {
$flag = 0;
}
}
谁能帮助我在这里做错了什么?
【问题讨论】:
-
$cols->item(4)->getAttribute('status') == 0
标签: php xml dom xml-parsing