使用 SimpleXMLElement 解析 XML 时出现问题

发布于 2024-12-01 00:42:58 字数 1459 浏览 2 评论 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);
 }

但是,我无法获取标签 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花ソ最美的依靠 2024-12-08 00:42:58

尝试使用 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);
}

另外,您可能想要包装try-catch 子句中的行 $xml = new SimpleXMLElement($sec_file); 以防 XML 文件由于某种原因无效。

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.

つ低調成傷 2024-12-08 00:42:58

尝试注册命名空间。
看看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文