开发者

Problem parsing XML using SimpleXMLElement

I'm using SimpleXMLElement to parse an XML file (XBRL is XML-based).

$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);
 }

However, I can't get the value o开发者_C百科f tag us-gaap:InterestExpenseDebt.

It is defined in the XML as follows:

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

The namespace in this XML file is

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

I added this, but it still doesn't work:

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

Any suggestions?

Thanks!


Try fetching the nodes using 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);
}

Also, you might want to wrap the line $xml = new SimpleXMLElement($sec_file); in a try-catch clause in case the XML file is invalid for some reason.


Try to register namespace. Look at http://www.php.net/manual/en/simplexmlelement.registerxpathnamespace.php . May be it help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜