如何使用php解析此XML [英] How to parse this XML using php
本文介绍了如何使用php解析此XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下XML(XBRL)文件
I have the following XML (XBRL) file
<xbrli:context id="I2010_ForwardContractsMember">
<xbrli:entity>
<xbrli:identifier scheme="http://www.sec.gov/CIK">0000027419</xbrli:identifier>
<xbrli:segment>
<xbrldi:explicitMember dimension="us-gaap:DerivativeByNatureAxis">us-gaap:ForwardContractsMember</xbrldi:explicitMember>
</xbrli:segment>
</xbrli:entity>
<xbrli:period>
<xbrli:instant>2011-01-29</xbrli:instant>
</xbrli:period>
</xbrli:context>
<xbrli:context id="D2010Q1">
<xbrli:entity>
<xbrli:identifier scheme="http://www.sec.gov/CIK">0000027419</xbrli:identifier>
</xbrli:entity>
<xbrli:period>
<xbrli:startDate>2010-01-31</xbrli:startDate>
<xbrli:endDate>2010-05-01</xbrli:endDate>
</xbrli:period>
</xbrli:context>
我想获取id ="D2010Q1"时的startDate和endDate值
I want to get the startDate and endDate value when id="D2010Q1"
我的代码正在跟踪
$xml = new SimpleXMLElement($sec_file);
$xml -> registerXPathNamespace('us-gaap', "http://fasb.org/us-gaap/2011-01-31");
foreach ($xml->xpath('//xbrli:context') as $item) {
if ($item -> attributes() -> id == 'D2010Q1') {
//print_r($item -> xpath('//xbrli:startDate'));
echo $item->xpath('//xbrli:startDate');
echo '<br>';
break;
}
}
我不知道如何获取startDate和endDate值. 有什么建议吗?
I don't know how to get startDate and endDate value. Any suggestions?
原始xml位于
http://www.sec.gov/存档/edgar/data/27419/000110465911031717/tgt-20110430.xml
推荐答案
我需要在文件中注册所有名称空间吗?如何获取startDate和endDate信息?
Do I need to register all the namespace in the file? How could I get startDate and endDate infor?
否,您只需要注册所需的名称空间,即http://www.xbrl.org/2003/instance
No you only have to register the namespaces you need i.e. http://www.xbrl.org/2003/instance
$xmldoc = new DOMDocument();
$xmldoc->load("http://www.sec.gov/Archives/edgar/data/27419/000110465911031717/tgt-20110430.xml");
$xpath = new DOMXPath($xmldoc);
$xpath->registerNamespace("xbrli", "http://www.xbrl.org/2003/instance");
$nodelist = $xpath->query("/xbrli:xbrl/xbrli:context[@id='D2010Q1']/xbrli:period"); // much faster than //xbrli:context and //xbrli:startDate
if($nodelist->length === 1)
{
$period = $nodelist->item(0);
$nodelist = $xpath->query("xbrli:startDate", $period);
$startDate = $nodelist->length === 1 ? $nodelist->item(0)->nodeValue : null;
$nodelist = $xpath->query("xbrli:endDate", $period);
$endDate = $nodelist->length === 1 ? $nodelist->item(0)->nodeValue : null;
printf("%s<br>%s", $startDate, $endDate);
}
else
; // not found or more than one <xbrli:context id='D2010Q1'><xbrli:period>
旧:
$xmldoc = new DOMDocument();
$xmldoc->load("http://www.sec.gov/Archives/edgar/data/27419/000110465911031717/tgt-20110430.xml");
$xpath = new DOMXPath($xmldoc);
$xpath->registerNamespace("xbrli", "http://www.xbrl.org/2003/instance");
$nodelist = $xpath->query("/xbrli:xbrl/xbrli:context[@id='D2010Q1']/xbrli:period/xbrli:startDate"); // much faster than //xbrli:context and //xbrli:startDate
if($nodelist->length === 1)
print $nodelist->item(0)->nodeValue;
else
; // not found or more than one <xbrli:context id='D2010Q1'><xbrli:period>
这篇关于如何使用php解析此XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文