Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 来自DOM对象的getAttribute未返回属性_Php_Xml_Getattribute - Fatal编程技术网

Php 来自DOM对象的getAttribute未返回属性

Php 来自DOM对象的getAttribute未返回属性,php,xml,getattribute,Php,Xml,Getattribute,我正在编写一个程序,从多个外部来源获取经济和社会统计数据,并将其纳入数据库(用于数据分析)。一些数据是XML格式的,要解析它,我需要识别XML文件中的元素/标记以及属性。为了识别属性,我尝试使用getAttribute 问题是:虽然getElementsByTagName可以工作,但getAttribute不能。尝试从单元格元素检索属性“Index”的值会返回“”,即使许多单元格元素中确实存在属性“Index”。没有错误,只是没有返回值 我花了很多天阅读PHP手册,研究互联网,试图找到一个解决方

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

问题是:虽然getElementsByTagName可以工作,但getAttribute不能。尝试从单元格元素检索属性“Index”的值会返回“”,即使许多单元格元素中确实存在属性“Index”。没有错误,只是没有返回值

我花了很多天阅读PHP手册,研究互联网,试图找到一个解决方案,但没有成功。 对getAttribute的返回值进行回显或var_dump显示它始终返回“”。 我没有将整个源代码放在下面,而是复制了一个更简单的版本来读取下面的XML文件,它将有同样的问题,即无法返回属性(在本例中为“Index”属性)


下面是XML文件的一个示例,它显示属性“Index”确实存在,即使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>

AAA
美联储理事会(美国)
H.15选定利率
百分比
未经季节性调整
美联储委员会已于2016年10月11日停止了该系列。更多信息,包括可能的替代系列,请访问http://www.federalreserve.gov/feeds/h15.html. 

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

在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>

AAA
美联储理事会(美国)
H.15选定利率
百分比
未经季节性调整
美联储委员会已于2016年10月11日停止了该系列。更多信息,包括可能的替代系列,请访问http://www.federalreserve.gov/feeds/h15.html. 
请尝试以下代码以获取命名空间为的属性值:

<?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>";


   }

}

在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>

AAA
美联储理事会(美国)
H.15选定利率
百分比
未经季节性调整
美联储委员会已于2016年10月11日停止了该系列。更多信息,包括可能的替代系列,请访问http://www.federalreserve.gov/feeds/h15.html. 
请尝试以下代码以获取命名空间为的属性值:

<?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>";


   }

}

经过进一步的研究,我找到了另一个遇到这个问题并设法解决的人。XML单元格标记/元素中的属性“Index”预先固定为“ss:”(根据上面的XML文件摘录
)。要使getAttribute正常工作,需要包括“ss:”,例如,正确的代码应该是
getAttribute('ss:Index')
,而不是
getAttribute('Index')


我不完全理解
getAttribute
如何识别属性,但它可能是在搜索前有空格的连续字符字符串,因此需要包括“ss:”

经过进一步研究,我找到了另一个遇到这个问题的人,并设法解决了这个问题。XML单元格标记/元素中的属性“Index”预先固定为“ss:”(根据上面的XML文件摘录
)。要使getAttribute正常工作,需要包括“ss:”,例如,正确的代码应该是
getAttribute('ss:Index')
,而不是
getAttribute('Index')


我不完全理解
getAttribute
如何识别属性,但它可能是在搜索前有空格的连续字符字符串,因此需要包括“ss:”

谢谢你指出我的错误。在从源代码(正确声明了$cell)重写这个简化的示例时,我一定有点草率。不过,经过修正后,问题依然存在。使用getAttribute仍然没有回报我已经编辑了上面的代码,并进行了更正,以避免在以后的Reviews中分散注意力作为更新,我尝试在每个$cell上进行var_转储,似乎说在将XML加载到DOM对象时忽略了属性。这是来自var_dump:public'attributes'=>string'(省略对象值)'(长度=22)如果是这种情况,那么问题是为什么在将XML文件加载到DOM对象时会去掉属性?我已经编辑了我的答案。希望这对你有帮助@谢谢你指出我的错误。在从源代码(正确声明了$cell)重写这个简化的示例时,我一定有点草率。不过,经过修正后,问题依然存在。使用getAttribute仍然没有回报我已经编辑了上面的代码,并进行了更正,以避免在以后的Reviews中分散注意力作为更新,我尝试在每个$cell上进行var_转储,似乎说在将XML加载到DOM对象时忽略了属性。这是来自var_dump:public'attributes'=>string'(省略对象值)'(长度=22)如果是这种情况,那么问题是为什么在将XML文件加载到DOM对象时会去掉属性?我已经编辑了我的答案。希望这对你有帮助@克里斯:我建议您也看看
DOMXpath::evaluate()
。使用Xpath使从DOM文档读取数据变得更加容易。我建议您也看看
DOMXpath::evaluate()
。使用Xpath使从DOM文档读取数据变得更加容易。