0

我尝试使用 foreach 循环遍历 SimpleXML 对象。我尝试使用 SimpleXMLIterator 获得相同的结果。

<?xml version="1.0" ?>

<SectionResults 
    xmlns:ns10="http://wgs.thomsonreuters.com/clear/api/search/court-ucc-search-extension/niem/1.0" 
    xmlns:ns11="http://wgs.thomsonreuters.com/clear/api/search/court-search-extension/niem/1.0" 
    xmlns:ns12="http://wgs.thomsonreuters.com/clear/api/report/report-realProperty-extension/niem/1.0" 
    xmlns:ns13="http://wgs.thomsonreuters.com/clear/api/report/report-asset-extension/niem/1.0" 
    xmlns:ns14="http://wgs.thomsonreuters.com/clear/api/report/report-business-commerce-extension/niem/1.0" 
    xmlns:ns15="http://wgs.thomsonreuters.com/clear/api/search/sanction-search-extension/niem/1.0" 
    xmlns:ns16="http://wgs.thomsonreuters.com/clear/api/report/report-bankruptcy-extension/niem/1.0" 
    xmlns:ns17="http://wgs.thomsonreuters.com/clear/api/report/report-personaldata-extension/niem/1.0" 
    xmlns:ns18="http://wgs.thomsonreuters.com/clear/api/search/license-prolicense-search-extension/niem/1.0" 
    xmlns:ns19="http://wgs.thomsonreuters.com/clear/api/search/person-search-extension/niem/1.0" 
    xmlns:ns2="http://niem.gov/niem/structures/2.0" 
    xmlns:ns20="http://wgs.thomsonreuters.com/clear/api/search/business-search-extension/niem/1.0" 
    xmlns:ns21="http://wgs.thomsonreuters.com/clear/api/report/report-driver-vehicle-extension/niem/1.0" 
    xmlns:ns22="http://wgs.thomsonreuters.com/clear/api/report/report-fraud-extension/niem/1.0" 
    xmlns:ns23="http://wgs.thomsonreuters.com/clear/api/search/court-lawsuits-search-extension/niem/1.0" 
    xmlns:ns24="http://wgs.thomsonreuters.com/clear/api/report/report-lawsuit-extension/niem/1.0" 
    xmlns:ns25="http://wgs.thomsonreuters.com/clear/api/search/license-drivers-search-extension/niem/1.0" 
    xmlns:ns26="http://wgs.thomsonreuters.com/clear/api/report/report-lienjudge-extension/niem/1.0" 
    xmlns:ns27="http://wgs.thomsonreuters.com/clear/api/search/court-bankruptcy-search-extension/niem/1.0" 
    xmlns:ns28="http://wgs.thomsonreuters.com/clear/api/search/court-lienjudge-search-extension/niem/1.0" 
    xmlns:ns29="http://wgs.thomsonreuters.com/clear/api/report/report-docket-extension/niem/1.0" 
    xmlns:ns3="http://niem.gov/niem/niem-core/2.0" 
    xmlns:ns30="http://wgs.thomsonreuters.com/clear/api/report/report-sanction-extension/niem/1.0" 
    xmlns:ns31="http://wgs.thomsonreuters.com/clear/api/report/report-license-extension/niem/1.0" 
    xmlns:ns32="http://wgs.thomsonreuters.com/clear/api/search/npi-search-extension/niem/1.0" 
    xmlns:ns33="http://wgs.thomsonreuters.com/clear/api/search/court-criminal-search-extension/niem/1.0" 
    xmlns:ns34="http://wgs.thomsonreuters.com/clear/api/report/report-npi-extension/niem/1.0" 
    xmlns:ns35="http://niem.gov/niem/appinfo/2.0" 
    xmlns:ns36="http://wgs.thomsonreuters.com/clear/api/report/1.0" 
    xmlns:ns37="http://wgs.thomsonreuters.com/clear/api/report/person-report/niem/1.0" 
    xmlns:ns38="http://wgs.thomsonreuters.com/clear/api/report/report-asset/niem/1.0" 
    xmlns:ns39="http://wgs.thomsonreuters.com/clear/api/report/report-bankruptcy/niem/1.0" 
    xmlns:ns4="http://wgs.thomsonreuters.com/clear/api/search/license-search-extension/niem/1.0" 
    xmlns:ns40="http://wgs.thomsonreuters.com/clear/api/report/report-business-commerce/niem/1.0" 
    xmlns:ns41="http://wgs.thomsonreuters.com/clear/api/report/report-crime/niem/1.0" 
    xmlns:ns42="http://wgs.thomsonreuters.com/clear/api/report/report-docket/niem/1.0" 
    xmlns:ns43="http://wgs.thomsonreuters.com/clear/api/report/report-driver-vehicle/niem/1.0" 
    xmlns:ns44="http://wgs.thomsonreuters.com/clear/api/report/report-fraud/niem/1.0" 
    xmlns:ns45="http://wgs.thomsonreuters.com/clear/api/report/report-lawsuit/niem/1.0" 
    xmlns:ns46="http://wgs.thomsonreuters.com/clear/api/report/report-license/niem/1.0" 
    xmlns:ns47="http://wgs.thomsonreuters.com/clear/api/report/report-lienjudge/niem/1.0" 
    xmlns:ns48="http://wgs.thomsonreuters.com/clear/api/report/report-personaldata/niem/1.0" 
    xmlns:ns49="http://wgs.thomsonreuters.com/clear/api/report/report-realProperty/niem/1.0" 
    xmlns:ns5="http://wgs.thomsonreuters.com/clear/api/search/license-search/niem/1.0" 
    xmlns:ns50="http://wgs.thomsonreuters.com/clear/api/search/person-search/niem/1.0" 
    xmlns:ns51="http://wgs.thomsonreuters.com/clear/api/search/phone-search/niem/1.0"
    xmlns:ns52="http://wgs.thomsonreuters.com/clear/api/search/business-search/niem/1.0" 
    xmlns:ns6="http://wgs.thomsonreuters.com/clear/api/report/report-crime-extension/niem/1.0"
    xmlns:ns7="http://wgs.thomsonreuters.com/clear/api/report/person-report-extension/niem/1.0" 
    xmlns:ns8="http://wgs.thomsonreuters.com/clear/api/search/phone-search-extension/niem/1.0" 
    xmlns:ns9="http://wgs.thomsonreuters.com/clear/api/search/court-search/niem/1.0">


    <SectionDetails>

        <ns37:UserSuppliedSection>

            <ns7:Address>

                <ns3:LocationCityName>
                    GREENBAY
                </ns3:LocationCityName>

                <ns3:LocationPostalCode>
                    54311
                </ns3:LocationPostalCode>

                <ns3:LocationStateUSPostalServiceCode>
                    WI
                </ns3:LocationStateUSPostalServiceCode>

                <ns3:StreetFullText>
                    123 Main St. 
                </ns3:StreetFullText>

            </ns7:Address>

            <ns7:PersonInfo>

                <ns3:PersonBirthDate>

                    <ns3:Date>
                        1982-08-12
                    </ns3:Date>

                </ns3:PersonBirthDate>

                <ns3:PersonName>

                    <ns3:PersonGivenName>
                        JOHN
                    </ns3:PersonGivenName>

                    <ns3:PersonMiddleName>
                        J
                    </ns3:PersonMiddleName>

                    <ns3:PersonSurName>
                        DOE
                    </ns3:PersonSurName>

                </ns3:PersonName>

                <ns3:PersonSSNIdentification>

                    <ns3:IdentificationID>
                        123456789
                    </ns3:IdentificationID>

                </ns3:PersonSSNIdentification>

            </ns7:PersonInfo>

        </ns37:UserSuppliedSection>

    </SectionDetails>
</SectionResults>

<SectionResults xmlns:ns10="http://wgs.thomsonreuters.com/clear/api/search/court-ucc-search-extension/niem/1.0" xmlns:ns11="http://wgs.thomsonreuters.com/clear/api/search/court-search-extension/niem/1.0" xmlns:ns12="http://wgs.thomsonreuters.com/clear/api/report/report-realProperty-extension/niem/1.0" xmlns:ns13="http://wgs.thomsonreuters.com/clear/api/report/report-asset-extension/niem/1.0" xmlns:ns14="http://wgs.thomsonreuters.com/clear/api/report/report-business-commerce-extension/niem/1.0" xmlns:ns15="http://wgs.thomsonreuters.com/clear/api/search/sanction-search-extension/niem/1.0" xmlns:ns16="http://wgs.thomsonreuters.com/clear/api/report/report-bankruptcy-extension/niem/1.0" xmlns:ns17="http://wgs.thomsonreuters.com/clear/api/report/report-personaldata-extension/niem/1.0" xmlns:ns18="http://wgs.thomsonreuters.com/clear/api/search/license-prolicense-search-extension/niem/1.0" xmlns:ns19="http://wgs.thomsonreuters.com/clear/api/search/person-search-extension/niem/1.0" xmlns:ns2="http://niem.gov/niem/structures/2.0" xmlns:ns20="http://wgs.thomsonreuters.com/clear/api/search/business-search-extension/niem/1.0" xmlns:ns21="http://wgs.thomsonreuters.com/clear/api/report/report-driver-vehicle-extension/niem/1.0" xmlns:ns22="http://wgs.thomsonreuters.com/clear/api/report/report-fraud-extension/niem/1.0" xmlns:ns23="http://wgs.thomsonreuters.com/clear/api/search/court-lawsuits-search-extension/niem/1.0" xmlns:ns24="http://wgs.thomsonreuters.com/clear/api/report/report-lawsuit-extension/niem/1.0" xmlns:ns25="http://wgs.thomsonreuters.com/clear/api/search/license-drivers-search-extension/niem/1.0" xmlns:ns26="http://wgs.thomsonreuters.com/clear/api/report/report-lienjudge-extension/niem/1.0" xmlns:ns27="http://wgs.thomsonreuters.com/clear/api/search/court-bankruptcy-search-extension/niem/1.0" xmlns:ns28="http://wgs.thomsonreuters.com/clear/api/search/court-lienjudge-search-extension/niem/1.0" xmlns:ns29="http://wgs.thomsonreuters.com/clear/api/report/report-docket-extension/niem/1.0" xmlns:ns3="http://niem.gov/niem/niem-core/2.0" xmlns:ns30="http://wgs.thomsonreuters.com/clear/api/report/report-sanction-extension/niem/1.0" xmlns:ns31="http://wgs.thomsonreuters.com/clear/api/report/report-license-extension/niem/1.0" xmlns:ns32="http://wgs.thomsonreuters.com/clear/api/search/npi-search-extension/niem/1.0" xmlns:ns33="http://wgs.thomsonreuters.com/clear/api/search/court-criminal-search-extension/niem/1.0" xmlns:ns34="http://wgs.thomsonreuters.com/clear/api/report/report-npi-extension/niem/1.0" xmlns:ns35="http://niem.gov/niem/appinfo/2.0" xmlns:ns36="http://wgs.thomsonreuters.com/clear/api/report/1.0" xmlns:ns37="http://wgs.thomsonreuters.com/clear/api/report/person-report/niem/1.0" xmlns:ns38="http://wgs.thomsonreuters.com/clear/api/report/report-asset/niem/1.0" xmlns:ns39="http://wgs.thomsonreuters.com/clear/api/report/report-bankruptcy/niem/1.0" xmlns:ns4="http://wgs.thomsonreuters.com/clear/api/search/license-search-extension/niem/1.0" xmlns:ns40="http://wgs.thomsonreuters.com/clear/api/report/report-business-commerce/niem/1.0" xmlns:ns41="http://wgs.thomsonreuters.com/clear/api/report/report-crime/niem/1.0" xmlns:ns42="http://wgs.thomsonreuters.com/clear/api/report/report-docket/niem/1.0" xmlns:ns43="http://wgs.thomsonreuters.com/clear/api/report/report-driver-vehicle/niem/1.0" xmlns:ns44="http://wgs.thomsonreuters.com/clear/api/report/report-fraud/niem/1.0" xmlns:ns45="http://wgs.thomsonreuters.com/clear/api/report/report-lawsuit/niem/1.0" xmlns:ns46="http://wgs.thomsonreuters.com/clear/api/report/report-license/niem/1.0" xmlns:ns47="http://wgs.thomsonreuters.com/clear/api/report/report-lienjudge/niem/1.0" xmlns:ns48="http://wgs.thomsonreuters.com/clear/api/report/report-personaldata/niem/1.0" xmlns:ns49="http://wgs.thomsonreuters.com/clear/api/report/report-realProperty/niem/1.0" xmlns:ns5="http://wgs.thomsonreuters.com/clear/api/search/license-search/niem/1.0" xmlns:ns50="http://wgs.thomsonreuters.com/clear/api/search/person-search/niem/1.0" xmlns:ns51="http://wgs.thomsonreuters.com/clear/api/search/phone-search/niem/1.0" xmlns:ns52="http://wgs.thomsonreuters.com/clear/api/search/business-search/niem/1.0" xmlns:ns6="http://wgs.thomsonreuters.com/clear/api/report/report-crime-extension/niem/1.0" xmlns:ns7="http://wgs.thomsonreuters.com/clear/api/report/person-report-extension/niem/1.0" xmlns:ns8="http://wgs.thomsonreuters.com/clear/api/search/phone-search-extension/niem/1.0" xmlns:ns9="http://wgs.thomsonreuters.com/clear/api/search/court-search/niem/1.0">
    <SectionName>
        SubjectSection
    </SectionName>
    <SectionStatus>
        COMPLETE
    </SectionStatus>
    <SectionRecordCount>
        1
    </SectionRecordCount>
    <SectionDetails>
        <ns37:SubjectSection>
            <ns7:PersonInfo>
                <ns7:SSNIssuanceText>
                    SSN issued in WI in 1981
                </ns7:SSNIssuanceText>
                <ns7:SSNStartYear>
                    1981
                </ns7:SSNStartYear>
                <ns3:PersonAgeDescriptionText>
                    50
                </ns3:PersonAgeDescriptionText>
                <ns3:PersonBirthDate>
                    <ns3:Date>
                        1900-01-12
                    </ns3:Date>
                </ns3:PersonBirthDate>
                <ns3:PersonName>
                    <ns3:PersonGivenName>
                        JOHN
                    </ns3:PersonGivenName>
                    <ns3:PersonMiddleName>
                        J
                    </ns3:PersonMiddleName>
                    <ns3:PersonSurName>
                        DOE
                    </ns3:PersonSurName>
                </ns3:PersonName>
                <ns3:PersonSSNIdentification>
                    <ns3:IdentificationID>
                        123-45-6789
                    </ns3:IdentificationID>
                    <ns3:IdentificationJurisdictionText>
                        CA
                    </ns3:IdentificationJurisdictionText>
                </ns3:PersonSSNIdentification>
            </ns7:PersonInfo>
        </ns37:SubjectSection>
    </SectionDetails>
</SectionResults>

如果我需要使用它们,所有命名空间都在 SectionResults 节点中定义。XML 的 var_dump 可以很好地返回 SectionName、SectionStatus 和 SectionRecordCount。但是,SectionDetails 作为另一个 SimpleXML 对象返回。我试过循环 $xml->SectionDetails->children('namespace'),但我无法获取任何信息。任何帮助将不胜感激。

4

1 回答 1

0

对于这样的多个命名空间,使用SimpleXML::children()并​​不是最佳的。xpath查询会更好地为您服务。例子:

$path = '/SectionResults/SectionDetails/ns37:UserSuppliedSection/ns7:Address/*';
foreach ($xml->xpath($path) as $e) {
    echo $e->getName() , ': ', trim($e);
    echo "\n";
}
于 2012-05-08T09:18:28.380 回答