2011-05-09 19 views
0

我有一个使用ID或类型的XML源。我需要选择特定节点并将其转换为变量。使用PHP SimpleXml从xmlfeed选择特定的ID

示例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> 

我一直在用这个提取数据:

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

谁能帮助?

非常感谢!

回答

0

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

您好,感谢您的回答。这转储我的工作表中的所有数据,但我怎么能去那里有“前利马”到$ startfinish和“很多东西”作为$ whatsincluded? – 2011-05-09 15:24:05

+0

看到更新的代码 – gaRex 2011-05-09 16:55:32

+0

嗨,感谢您的编辑。然而这只是产生一个列表。我需要实际上能够将$ startfinish分配给'ex lima'等。任何想法?谢谢! – 2011-05-09 19:09:11