【问题标题】:Problem parsing XML using SimpleXMLElement使用 SimpleXMLElement 解析 XML 的问题
【发布时间】:2011-08-19 20:08:07
【问题描述】:

我正在使用 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);
 }

但是,我无法获取标签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");

有什么建议吗?

谢谢!

【问题讨论】:

    标签: php xml xbrl


    【解决方案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 文件因某种原因无效。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-04
        • 1970-01-01
        • 1970-01-01
        • 2016-04-25
        • 2014-08-30
        相关资源
        最近更新 更多