开发者

Selecting specific id from xmlfeed using PHP SimpleXml

I have an XML Feed which is using ID's or Types. I need to select the specific node and convert it to a variable.

Example XML Feed:

<tour>
<tourName>Amazon Riverboat Adventure</tourName>
<dossierCo开发者_Go百科de>PVIIA</dossierCode>
<tripDetails>
<tripDetail type="StartFinish">ex Lima</tripDetail>
<tripDetail type="What's Included">Lots of stuff </tripDetail>
</tripDetails>

I have been extracting this data by using:

<?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)
?>

However I am unsure how I can extract the <tripDetail type="StartFinish"> as $startfinish.

Can anyone help?

Many thanks!


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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜