SimpleXML跳过属性

时间:2018-04-19 11:07:20

标签: php simplexml xmlreader

测试XML:

<?xml version="1.0" encoding="UTF-8"?>
<Transfer>
    <ABR recordLastUpdatedDate="20180329" replaced="N">
        <ABN status="ACT" ABNStatusFromDate="20000214">80007321682</ABN>
        <EntityType>
            <EntityTypeInd>PUB</EntityTypeInd>
            <EntityTypeText>Australian Public Company</EntityTypeText>
        </EntityType>
        <MainEntity>
            <NonIndividualName type="MN">
                <NonIndividualNameText>BLACK CABS COMBINED PTY LTD</NonIndividualNameText>
            </NonIndividualName>
            <BusinessAddress>
                <AddressDetails>
                    <State>VIC</State>
                    <Postcode>3166</Postcode>
                </AddressDetails>
            </BusinessAddress>
        </MainEntity>
    </ABR>
</Transfer>

PHP脚本:

$f='test.xml';
$reader=new XMLReader();
$reader->open($f);
while($reader->read()){
    if($reader->nodeType==XMLReader::ELEMENT && $reader->name=='ABR'){
        $doc=new DOMDocument('1.0','UTF-8');
        $xml=simplexml_import_dom($doc->importNode($reader->expand(),true));
        print_r($xml);
    }
}
$reader->close();

PHP输出:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [recordLastUpdatedDate] => 20180329
            [replaced] => N
        )

    [ABN] => 80007321682
    [EntityType] => SimpleXMLElement Object
        (
            [EntityTypeInd] => PUB
            [EntityTypeText] => Australian Public Company
        )

    [MainEntity] => SimpleXMLElement Object
        (
            [NonIndividualName] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [type] => MN
                        )

                    [NonIndividualNameText] => BLACK CABS COMBINED PTY LTD
                )

            [BusinessAddress] => SimpleXMLElement Object
                (
                    [AddressDetails] => SimpleXMLElement Object
                        (
                            [State] => VIC
                            [Postcode] => 3166
                        )

                )

        )

)

问题:

ABN元素(statusABNStatusFromDate)的属性不在输出中,即使其他属性也是如此。

请帮助我理解为什么缺少这些属性。

PS - 虚拟文字所以SO没有给我警告我的帖子主要是代码

1 个答案:

答案 0 :(得分:1)

答案:getInitialProps并不打算用于显示SimpleXML对象。

我可以通过print_r直接访问该属性。