How to parse this XML using php

落花浮王杯 提交于 2020-01-05 05:04:01

问题


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>

I want to get the startDate and endDate value when id="D2010Q1"

My codes is following

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

I don't know how to get startDate and endDate value. Any suggestions?

The original xml is located at

http://www.sec.gov/Archives/edgar/data/27419/000110465911031717/tgt-20110430.xml


回答1:


Edit:

Do I need to register all the namespace in the file? How could I get startDate and endDate infor?

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>

Old:

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



回答2:


You'll need to register the xbrli namespace as well, similar to what you've done with us-gaap. From WikiPedia I found this xmlns:xbrli="http://www.xbrl.org/2003/instance"



来源:https://stackoverflow.com/questions/7149581/how-to-parse-this-xml-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!