Выбор конкретного идентификатора из xmlfeed с использованием PHP SimpleXml - PullRequest
0 голосов
/ 09 мая 2011

У меня есть лента XML, которая использует идентификаторы или типы.Мне нужно выбрать конкретный узел и преобразовать его в переменную.

Пример XML Feed:

<tour>
<tourName>Amazon Riverboat Adventure</tourName>
<dossierCode>PVIIA</dossierCode>
<tripDetails>
<tripDetail type="StartFinish">ex Lima</tripDetail>
<tripDetail type="What's Included">Lots of stuff </tripDetail>
</tripDetails>

Я извлекаю эти данные, используя:

<?php
if(!$xml=simplexml_load_file('xmlfeed.xml')){
    trigger_error('Error reading XML file',E_USER_ERROR);
}
foreach ($xml->tourName as $tourname)
foreach ($xml->dossierCode as $agentcode)
?>

Однако я не уверен, как извлечь <tripDetail type="StartFinish"> как $ startfinish.

Кто-нибудь может помочь?

Большое спасибо!

1 Ответ

0 голосов
/ 09 мая 2011

http://www.php.net/manual/en/simplexmlelement.attributes.php

<?php
echo $xmlAsString = <<<XML
<tour>
<tourName>Amazon Riverboat Adventure</tourName>
<dossierCode>PVIIA</dossierCode>
<tripDetails>
<tripDetail type="StartFinish">ex Lima</tripDetail>
<tripDetail type="What's Included">Lots of stuff</tripDetail>
</tripDetails>
</tour>


XML;
$xml = simplexml_load_string($xmlAsString);
foreach ($xml->tourName as $tourname) {
    var_dump($tourname);
}
foreach ($xml->dossierCode as $agentcode) {
    var_dump($agentcode);
}

foreach ($xml->tripDetails as $tripDetails) {
    foreach ($tripDetails as $tripDetail) {
        $attributes = $tripDetail->attributes();
        echo 'Content of the type: ' . $attributes['type'] . PHP_EOL .
             'Content of the whole tag: ' . $tripDetail  . PHP_EOL;
    }
}
...