-1

可能重复:
foreach 和 simplexml

我的 XML 文档加载正确,但我不确定如何为提要中给出的每个项目创建一个简单的 foreach[PictureHref] [Title][PriceDisplay]加载。

我在文档中找不到明确的示例。

XML 示例

目前我的 PHP 代码包括以下内容:

$mainUrl = 'http://api.trademe.co.nz/v1/Member/{id}/Listings/All.xml';
$xmlFeed = simplexml_load_file($mainUrl);

XML

<Listings xmlns="http://api.trademe.co.nz/v1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <TotalCount>1</TotalCount>
    <Page>1</Page>
    <PageSize>1</PageSize>
    <List>
        <Listing>
            <ListingId>527496168</ListingId>
            <Title>Clifftop Resort style Living with stunning aspect</Title>
            <Category>0350-5748-3399-</Category>
            <StartPrice>0</StartPrice>
            <StartDate>2012-10-26T21:24:47.073Z</StartDate>
            <EndDate>2012-12-21T21:24:47.073Z</EndDate>
            <ListingLength i:nil="true" />
            <HasGallery>true</HasGallery>
            <AsAt>2012-10-28T22:48:47.409946Z</AsAt>
            <CategoryPath>/Trade-me-property/Residential/For-sale</CategoryPath>
            <PictureHref>http://images.trademe.co.nz/photoserver/thumb/10/239043710.jpg</PictureHref>
            <RegionId>2</RegionId>
            <Region>Auckland</Region>
            <Suburb>North Shore</Suburb>
            <NoteDate>1970-01-01T00:00:00Z</NoteDate>
            <ReserveState>NotApplicable</ReserveState>
            <IsClassified>true</IsClassified>
            <GeographicLocation>
                <Latitude>-36.5681333</Latitude>
                <Longitude>174.6936265</Longitude>
                <Northing>5951700</Northing>
                <Easting>1751547</Easting>
                <Accuracy>Address</Accuracy>
            </GeographicLocation>
            <PriceDisplay>To be auctioned</PriceDisplay>
        </Listing>
    </List>
</Listings>
4

2 回答 2

1

所有你需要的是

$url = simplexml_load_file("__YOUR__URL___");
$listing = $url->List->Listing;

echo "<pre>";
echo $listing->PictureHref, PHP_EOL;
echo $listing->Title, PHP_EOL;
echo $listing->PriceDisplay, PHP_EOL;
于 2012-10-28T22:52:51.737 回答
0

虽然我建议使用 DOMDocument 而不是 SimpleXML,但下面是您在 SimpleXML 中的做法:

$data = array();

foreach($xmlFeed->List as $item) {
    $data[] = array(
        (string) $item->Listing->PictureHref,
        (string) $item->Listing->Title,
        (string) $item->Listing->PriceDisplay
    );
}

你可以在 CodePad上看到它(有点,我不得不改变一些东西)

于 2012-10-28T22:52:33.030 回答