如何使用Php解析此XML(XBRL)

时间:2017-06-30 11:35:47

标签: php xml domparser xbrl

我是PHP的新手,我需要解析一个XML文件,对其进行一些更改并编写它。

该文件如下所示:

<xbrli:xbrl xsi:schemaLocation="http://xbrl.org/2006/xbrldi http://www.xbrl.org/2006/xbrldi-2006.xsd"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:odrpt.0001.list.req.02.00="http://sbr.gov.au/rprt/ato/odrpt/odrpt.0001.list.request.02.00.report"
                    xmlns:ref="http://www.xbrl.org/2006/ref"
                    xmlns:dtyp.02.24="http://sbr.gov.au/fdtn/sbr.02.24.dtyp"
                    xmlns:pyid.02.10="http://sbr.gov.au/icls/py/pyid/pyid.02.10.data"
                    xmlns:emsup.02.08="http://sbr.gov.au/icls/em/emsup/emsup.02.08.data"
                    xmlns:emsup.02.20="http://sbr.gov.au/icls/em/emsup/emsup.02.20.data"
                    xmlns:xbrldt="http://xbrl.org/2005/xbrldt"
                    xmlns:SqNumDim.02.01_typedelement="http://sbr.gov.au/dims/SqNumDim.02.01.dims"
                    xmlns:pyde.02.20="http://sbr.gov.au/icls/py/pyde/pyde.02.20.data"
                    xmlns:dtyp.02.00="http://sbr.gov.au/fdtn/sbr.02.00.dtyp"
                    xmlns:pyid.02.00="http://sbr.gov.au/icls/py/pyid/pyid.02.00.data"
                    xmlns:link="http://www.xbrl.org/2003/linkbase"
                    xmlns:xlink="http://www.w3.org/1999/xlink"
                    xmlns:odrpt.0001.prv.02.00="http://sbr.gov.au/rprt/ato/odrpt/odrpt.0001.private.02.00.module"
                    xmlns:iso4217="http://www.xbrl.org/2003/iso4217"
                    xmlns:pyde.02.00="http://sbr.gov.au/icls/py/pyde/pyde.02.00.data"
                    xmlns:pyde.02.08="http://sbr.gov.au/icls/py/pyde/pyde.02.08.data"
                    xmlns:tech.01.02="http://sbr.gov.au/fdtn/sbr.01.02.tech"
                    xmlns:xbrldi="http://xbrl.org/2006/xbrldi"
                    xmlns:RprtPyType.02.13="http://sbr.gov.au/dims/RprtPyType.02.13.dims"
                    xmlns:tech.01.03="http://sbr.gov.au/fdtn/sbr.01.03.tech"
                    xmlns:xbrli="http://www.xbrl.org/2003/instance"
                    xmlns:pyin.02.02="http://sbr.gov.au/icls/py/pyin/pyin.02.02.data"
                    xmlns:pyde.02.12="http://sbr.gov.au/icls/py/pyde/pyde.02.12.data"
                    xmlns:pyid.02.03="http://sbr.gov.au/icls/py/pyid/pyid.02.03.data">
                    <link:schemaRef xlink:type="simple" xlink:href="http://sbr.gov.au/taxonomy/sbr_au_reports/ato/usmat/usmat_0001/usmat.0001.list.request.02.00.report.xsd"/>
                    <xbrli:context id="Context_Duration_ReportingParty">
                        <xbrli:entity>
                            <xbrli:identifier scheme="http://www.abr.gov.au/abn">111111111</xbrli:identifier>
                            <xbrli:segment>
                                <xbrldi:explicitMember dimension="RprtPyType.02.13:ReportPartyTypeDimension">RprtPyType.02.13:ReportingParty</xbrldi:explicitMember>
                            </xbrli:segment>
                        </xbrli:entity>
                        <xbrli:period>
                            <xbrli:startDate>2014-06-01</xbrli:startDate>
                            <xbrli:endDate>2014-06-01</xbrli:endDate>
                        </xbrli:period>
                    </xbrli:context>
                    <xbrli:context id="Context_Duration_SuperFundMember">
                        <xbrli:entity>
                            <xbrli:identifier scheme="http://www.ato.gov.au/tfn">11111111</xbrli:identifier>
                            <xbrli:segment>
                                <xbrldi:explicitMember dimension="RprtPyType.02.13:ReportPartyTypeDimension">RprtPyType.02.13:SuperFundMember</xbrldi:explicitMember>
                            </xbrli:segment>
                        </xbrli:entity>
                        <xbrli:period>
                            <xbrli:startDate>2015-06-01</xbrli:startDate>
                            <xbrli:endDate>2014-06-01</xbrli:endDate>
                        </xbrli:period>
                    </xbrli:context>
                    <pyde.02.00:OrganisationNameDetails.OrganisationalName.Text contextRef="Context_Duration_ReportingParty">CCCorp</pyde.02.00:OrganisationNameDetails.OrganisationalName.Text>
                    <pyde.02.00:PersonNameDetails.FamilyName.Text contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:PersonNameDetails.FamilyName.Text>
                    <pyde.02.00:PersonNameDetails.GivenName.Text contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:PersonNameDetails.GivenName.Text>
                    <pyde.02.00:PersonNameDetails.OtherGivenName.Text contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:PersonNameDetails.OtherGivenName.Text>
                    <pyde.02.12:PersonDemographicDetails.Birth.DayofMonth contextRef="Context_Duration_SuperFundMember">---18</pyde.02.12:PersonDemographicDetails.Birth.DayofMonth>
                    <pyde.02.12:PersonDemographicDetails.Birth.Month contextRef="Context_Duration_SuperFundMember">--12</pyde.02.12:PersonDemographicDetails.Birth.Month>
                    <pyde.02.12:PersonDemographicDetails.Birth.Year contextRef="Context_Duration_SuperFundMember">1960</pyde.02.12:PersonDemographicDetails.Birth.Year>
                    <pyid.02.10:Identifiers.SuperannuationMemberAccount.Identifier contextRef="Context_Duration_SuperFundMember">true</pyid.02.10:Identifiers.SuperannuationMemberAccount.Identifier>
                    <pyde.02.00:AddressDetails.Line1.Text contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:AddressDetails.Line1.Text>
                    <pyde.02.00:AddressDetails.Line2.Text contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:AddressDetails.Line2.Text>
                    <pyde.02.00:AddressDetails.LocalityName.Text contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:AddressDetails.LocalityName.Text>
                    <pyde.02.00:AddressDetails.Postcode.Text contextRef="Context_Duration_SuperFundMember">3350</pyde.02.00:AddressDetails.Postcode.Text>
                    <pyde.02.00:AddressDetails.StateOrTerritory.Code contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:AddressDetails.StateOrTerritory.Code>
                    <pyde.02.08:AddressDetails.Country.Code contextRef="Context_Duration_SuperFundMember">au</pyde.02.08:AddressDetails.Country.Code>
                    <emsup.02.08:SuperannuationFundDetails.UniqueSuperannuationIdentifier.Identifier contextRef="Context_Duration_SuperFundMember">abc1234ab</emsup.02.08:SuperannuationFundDetails.UniqueSuperannuationIdentifier.Identifier>
                </xbrli:xbrl>

您可以在此处查看:https://codebeautify.org/xmlviewer/cbfe5ebd

我使用了一些示例尝试过DOM解析器,但我无法解析字段 喜欢:

<pyde.02.00:AddressDetails.Postcode.Text>

如果需要任何其他信息,请与我们联系。

2 个答案:

答案 0 :(得分:1)

如果您使用simplexml_load_string之类的内容加载数据,则以下内容将完成此任务...

    $xml = simplexml_load_string( '
            <xbrli:xbrl xsi:schemaLocation="http://xbrl.org/2006/xbrldi http://www.xbrl.org/2006/xbrldi-2006.xsd"
                       ... 
            </xbrli:xbrl>
');


foreach($xml->xpath('//pyde.02.00:AddressDetails.Postcode.Text') as $postCode) {
    print_r($postCode);
}

您需要添加XML文档的其余部分,但这只是为了显示主要代码而不是重新列出整个xml。 主要的是XPath,你可以看到它有命名空间(pyde.02.00)以及元素名称来定位你的特定位。然后,您应该能够使用自己的代码处理这些元素。

编辑: 要修改值,您需要更改元素的相应部分。所以上面打印出来......

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [contextRef] => Context_Duration_SuperFundMember
        )

    [0] => 3350
)

所以,如果你在代码中

$postCode = $xml->xpath('//pyde.02.00:AddressDetails.Postcode.Text')[0];
$postCode[0] = "3351";

这导致元素现在

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [contextRef] => Context_Duration_SuperFundMember
        )

    [0] => 3351
)

如果要将其保存到文件中,则

$xml->asXml( $fileName );

答案 1 :(得分:1)

使用以下代码:

       $xmldoc = new DOMDocument();
            $xmldoc->load("myFile.xml");
            $xpath = new DOMXPath($xmldoc);
            $xpath->registerNamespace("xbrli",  "http://www.xbrl.org/2003/instance");
            $xpath->registerNamespace("pyde.02.00", "http://sbr.gov.au/icls/py/pyde/pyde.02.00.data");

            $organizationNameList = $xpath->query("/xbrli:xbrl/pyde.02.00:OrganisationNameDetails.OrganisationalName.Text[@contextRef='    Context_Duration_ReportingParty']");
            if($organizationNameList->length != 0){
                $orgNode = $organizationNameList->item(0);
                $orgName = $organizationNameList->length === 1 ? $organizationNameList->item(0)->nodeValue : null;
                echo $orgName;
            }

使用以下代码我可以使用特定查询

读取值