【问题标题】:getAttribute from DOM object not returning attributes来自 DOM 对象的 getAttribute 不返回属性
【发布时间】:2017-02-07 11:36:09
【问题描述】:

我正在编写一个程序,该程序从多个外部来源获取经济和社会统计数据,并将它们提取到数据库中(用于数据分析)。一些数据采用 XML 格式,为了解析它,我需要识别 XML 文件中的元素/标签以及属性。为了识别我尝试使用 getAttribute 的属性。

问题:虽然 getElementsByTagName 有效,但 getAttribute 无效。尝试从单元格元素中检索属性“索引”的值会返回“”,即使属性“索引”确实存在于许多单元格元素中。没有错误,只是没有返回值。

我花了几天时间阅读 PHP 手册并研究 Internet 以尝试找到解决方案,但没有成功。 getAttribute 的返回值上的回显或 var_dump 表明它始终返回“”。 我没有放整个源代码,而是复制了一个读取下面 XML 文件的更简单版本,它会遇到同样的问题,即无法返回属性(在本例中为“索引”属性)。

<?php

// Creates new DOMDocument
$dom = new DOMDocument();
// Loads XML file into DOMDocument
$dom->load('FRED_formatted_list.xml');

// Stores all the instances of the Row tag into $rows
$rows = $dom->getElementsByTagName('Row');

// Iterates through all the instances of the Row tag
foreach($rows as $row) {

// Stores all the instances of the Cell tag into $cells
$cells = $row->getElementsByTagName('Cell');

// Iterates through all the instances of the Cell tag
foreach($cells as $cell) {

    // Checks if the Index attribute exists in the cell tag
    if($cell->hasAttribute('Index')) {
        // Stores the value of any instances of the Index attribute
        $attr = $cell->getAttribute('Index');
        // Prints the value of any instances of the Index attribute to screen
        echo "Value of index attribute: " . $attr . "<br>";

    }
    // Check that the cell tags have been properly identified in the DOM Object
    echo $cell->nodeValue . "<br>";
    // Double checks whether any index values are even found and stored in $attr
    var_dump($attr) . "<br>";
}
}
?>

这是一个 XML 文件的示例,它显示属性“索引”确实存在,即使 getAttributes 没有返回:

<Row>
    <Cell><Data ss:Type="String">AAA</Data></Cell>
    <Cell ss:Index="3"><Data ss:Type="String">Board of Governors of the Federal Reserve System (US)</Data></Cell>
    <Cell><Data ss:Type="String">H.15 Selected Interest Rates</Data></Cell>
    <Cell><Data ss:Type="String">Percent</Data></Cell>
    <Cell><Data ss:Type="String">Not Seasonally Adjusted</Data></Cell>
    <Cell><Data ss:Type="String">The Federal Reserve Board has discontinued this series as of October 11, 2016. More information, including possible alternative series, can be found at http://www.federalreserve.gov/feeds/h15.html. </Data></Cell>
</Row>

任何帮助将不胜感激。我将总结解决方案并重新发布以帮助他人。

【问题讨论】:

  • 我建议你也看看DOMXpath::evaluate()。使用 Xpath 可以更轻松地从 DOM 文档中读取数据。

标签: php xml getattribute


【解决方案1】:

经过进一步研究,我发现其他人遇到了这个问题并设法解决了它。 XML 单元标记/元素中的属性“索引”以“ss:”为前缀(根据上面的 XML 文件提取 &lt;Cell ss:Index="3"&gt;&lt;Data ss:Type="String"&gt;)。要使 getAttribute 起作用,需要包含“ss:”,例如正确的代码是 getAttribute('ss:Index') 而不是 getAttribute('Index')
我不完全理解getAttribute 是如何识别一个属性的,但它可能是搜索一个前面带有空格的连续字符的字符串,因此需要包含'ss:'。

【讨论】:

    【解决方案2】:

    在 xml 中定义命名空间:

    <Row xmlns:ss="something">
      <Cell><Data ss:Type="String">AAA</Data></Cell>
      <Cell ss:Index="3"><Data ss:Type="String">Board of Governors of the Federal Reserve System (US)</Data></Cell>
      <Cell><Data ss:Type="String">H.15 Selected Interest Rates</Data></Cell>
      <Cell><Data ss:Type="String">Percent</Data></Cell>
      <Cell><Data ss:Type="String">Not Seasonally Adjusted</Data></Cell>
      <Cell><Data ss:Type="String">The Federal Reserve Board has discontinued this series as of October 11, 2016. More information, including possible alternative series, can be found at http://www.federalreserve.gov/feeds/h15.html. </Data></Cell>
    </Row>
    

    尝试使用以下代码获取具有命名空间的属性值:

    <?php
    
      // Creates new DOMDocument
      $dom = new DOMDocument();
      // Loads XML file into DOMDocument
      $dom->load('FRED_formatted_list.xml');
    
      // Stores all the instances of the Row tag into $rows
      $rows = $dom->getElementsByTagName('Row');
      $attr ='';
      // Iterates through all the instances of the Row tag
      foreach($rows as $row) {
    
         // Stores all the instances of the Cell tag into $cells
         $cells = $row->getElementsByTagName('Cell');
    
        // Iterates through all the instances of the Cell tag
        foreach($cells as $cell) {
            // Checks if the Index attribute exists in the cell tag
           if($cell->attributes->getNamedItem('Index')) {
               // Stores the value of any instances of the Index attribute
               $attr = $cell->attributes->getNamedItem('Index')->nodeValue;
               // Prints the value of any instances of the Index attribute to screen
                echo "Value of index attribute: " . $attr . "<br>";
    
            }
         // Check that the cell tags have been properly identified in the DOM Object
         echo $cell->nodeValue . "<br>";
         // Double checks whether any index values are even found and stored in $attr
         var_dump($attr) . "<br>";
    
    
       }
    
    }
    

    【讨论】:

    • 感谢您指出我的错误。从源代码(正确说明了 $cell )重写这个简化示例时,我一定有点草率。改正后问题依然存在。使用getAttribute还是没有回报
    • 我已经编辑了上面的代码并进行了更正,以避免在以后的评论中分心
    • 作为更新,我在每个 $cell 上尝试了一个 var_dump,似乎在将 XML 加载到 DOM 对象时省略了属性。这是来自 var_dump: public 'attributes' => string '(object value removed)' (length=22) 如果是这种情况,那么问题是为什么在将 XML 文件加载到 DOM 对象时会删除属性?
    • 我已经编辑了我的答案。希望这会帮助你。 @ChrisE
    猜你喜欢
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多