使用 SimpleXMLElement 解析 XML 时出现问题
我正在使用 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");
有什么建议吗?
谢谢!
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 of 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 xpath 获取节点:
另外,您可能想要包装try-catch 子句中的行
$xml = new SimpleXMLElement($sec_file);
以防 XML 文件由于某种原因无效。Try fetching the nodes using xpath:
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.尝试注册命名空间。
看看 http://www.php.net/manual/en/simplexmlelement.registerxpathnamespace .php 。也许有帮助。
Try to register namespace.
Look at http://www.php.net/manual/en/simplexmlelement.registerxpathnamespace.php . May be it help.