如何使用SimpleXml获取XML-Namespace-Attribute?

时间:2014-05-30 19:38:34

标签: php xml simplexml

我尝试使用Immobilienscout24-API将我的客户的产品送到他的主页。

我获得了一个XML结果,其格式为here

使用

解析此结果后
$obj = new SimpleXMLElement($myXmlString);

属性xsi:元素realEstateElement的类型丢失。我该如何访问该元素?

谢谢!

1 个答案:

答案 0 :(得分:0)

<?php

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<realestates:realEstates xmlns:ns2="http://rest.immobilienscout24.de/schema/platform/gis/1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:common="http://rest.immobilienscout24.de/schema/common/1.0" xmlns:realestates="http://rest.immobilienscout24.de/schema/offer/realestates/1.0">
    <Paging>
        <next xlink:href="http://rest.sandbox-immobilienscout24.de/restapi/api/offer/v1.0/user/me/realestate?pagenumber=2"/>
        <pageNumber>1</pageNumber>
        <pageSize>20</pageSize>
        <numberOfPages>12</numberOfPages>
        <numberOfHits>222</numberOfHits>
    </Paging>
    <realEstateList>
        <realEstateElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:offerlistelement="http://rest.immobilienscout24.de/schema/offer/listelement/1.0" xsi:type="offerlistelement:OfferHouseRent" xlink:href="http://rest.sandbox-immobilienscout24.de/restapi/api/offer/v1.0/user/me/realestate/62283354" modification="2013-12-05T09:13:45.000+01:00" creation="2013-12-05T09:13:37.000+01:00" publishDate="2013-12-07T20:37:38.333+01:00" id="62283354">
            <externalId>62283354</externalId>
            <title>Unterbach Haus</title>
            <address>
                <street>Eichenwand</street>
                <houseNumber>2</houseNumber>
                <postcode>40627</postcode>
                <city>Düsseldorf</city>
                <geoHierarchy>
                    <continent>
                        <geoCodeId>1</geoCodeId>
                        <fullGeoCodeId>1</fullGeoCodeId>
                    </continent>
                    <country>
                        <geoCodeId>276</geoCodeId>
                        <fullGeoCodeId>1276</fullGeoCodeId>
                    </country>
                    <region>
                        <geoCodeId>10</geoCodeId>
                        <fullGeoCodeId>1276010</fullGeoCodeId>
                    </region>
                    <city>
                        <geoCodeId>12</geoCodeId>
                        <fullGeoCodeId>1276010012</fullGeoCodeId>
                    </city>
                    <quarter>
                        <geoCodeId>57</geoCodeId>
                        <fullGeoCodeId>1276010012057</fullGeoCodeId>
                    </quarter>
                    <neighbourhood>
                        <geoCodeId>5111000008401</geoCodeId>
                    </neighbourhood>
                </geoHierarchy>
            </address>
            <realEstateState>ACTIVE</realEstateState>
            <common:publishChannels>
                <publishChannel id="10000" title="Immobilienscout24"/>
                <publishChannel id="10001" title="Homepage"/>
            </common:publishChannels>
            <price>
                <value>45444</value>
                <currency>EUR</currency>
            </price>
            <livingSpace>440</livingSpace>
            <plotArea>460</plotArea>
            <numberOfRooms>5</numberOfRooms>
            <builtInKitchen>false</builtInKitchen>
        </realEstateElement>
</realEstateList>
</realestates:realEstates>
XML;

$sxml = new SimpleXMLElement($xml);
var_dump($sxml->realEstateList->realEstateElement->attributes('xsi', true)->type);

http://www.php.net/manual/en/simplexmlelement.attributes.php评论可能有用;)

相关问题