0

我正在使用 SimpleXMLElement 解析 XML 文件(XBRL 是基于 XML 的)。

$url = 'http://sec.gov/Archives/edgar/data/104169/000119312511158587/wmt-20110430.xml';
$sec_file = file_get_contents($url) or die('Could not access file: $url');
$xml = new SimpleXMLElement($sec_file);
foreach($xml->{'us-gaap:InterestExpenseDebt'} as $item) {
     print_r($item);
 }

但是,我无法获得 tag 的值us-gaap:InterestExpenseDebt

它在 XML 中定义如下:

<us-gaap:InterestExpenseDebt contextRef="Duration_2_1_2010_To_4_30_2010" unitRef="Unit12" decimals="-6">455000000</us-gaap:InterestExpenseDebt>
<us-gaap:InterestExpenseDebt contextRef="Duration_2_1_2011_To_4_30_2011" unitRef="Unit12" decimals="-6">491000000</us-gaap:InterestExpenseDebt>

此 XML 文件中的命名空间是

<xbrli:xbrl xmlns:cvs="http://www.info.cvscaremark.com/20110630" xmlns:link="http://www.xbrl.org/2003/linkbase" xmlns:us-gaap="http://fasb.org/us-gaap/2011-01-31" xmlns:xbrldi="http://xbrl.org/2006/xbrldi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xbrli="http://www.xbrl.org/2003/instance" xmlns:iso4217="http://www.xbrl.org/2003/iso4217" xmlns:dei="http://xbrl.sec.gov/dei/2011-01-31" xmlns:xlink="http://www.w3.org/1999/xlink">

我添加了这个,但它仍然不起作用:

$xml->registerXPathNamespace('us-gaap', "http://fasb.org/us-gaap/2011-01-31");

有什么建议么?

谢谢!

4

2 回答 2

1

尝试注册命名空间。查看http://www.php.net/manual/en/simplexmlelement.registerxpathnamespace.php。可能会有帮助。

于 2011-08-19T20:17:37.683 回答
1

尝试使用xpath获取节点:

$url = 'http://sec.gov/Archives/edgar/data/104169/000119312511158587/wmt-20110430.xml';
$sec_file = file_get_contents($url) or die('Could not access file: $url');
$xml = new SimpleXMLElement($sec_file);
$xml->registerXPathNamespace('us-gaap', "http://fasb.org/us-gaap/2011-01-31");

foreach($xml->xpath('//us-gaap:InterestExpenseDebt') as $item) {
     print_r($item);
}

此外,您可能希望将该行包装$xml = new SimpleXMLElement($sec_file);在 try-catch 子句中,以防 XML 文件由于某种原因无效。

于 2011-08-19T22:02:30.090 回答